# ODE solver events in matlab



## Hans87 (Jun 19, 2011)

Hey

I'm new to this community, I've got a problem with a matlab script I'm making and was therefore hoping there was someone who could help me.

I've made a program in matlab which does Kinematic ray tracing in a velocity model.
I use the ode45 solver in order to compute the differential equations. At the moment I use and if + elseif statement in order to stop computations once the ray reaches the surface like like shown below.

dz(1) and dz(2) is the movement along the x and z direction in the model.
function dz =raytrace(t,z)
global v dx dz

dz = zeros(4,1);
if z(2) < 0;
vel = interp2(3.5:0.01:20,0:-0.01:-10,v,z(1),z(2),'*linear');
v2 = vel^2;
v1 = 1/vel;
vx = interp2(3.5:0.01:20,0:-0.01:-10,fx,z(1),z(2),'*linear');
vz = interp2(3.5:0.01:20,0:-0.01:-10,fz,z(1),z(2),'*linear');
dz(1) = v2*z(3); 
dz(2) = v2*z(4);
dz(3) = -v1*vx;
dz(4) = -v1*vz;
elseif z(2) > 0;
return 
end

My goal is to make this function more elegant and hopefully faster by removing the if and elseif statement. I intend to do this by implementing an Event which stops the integration when the ray reaches the surface. 

What I've got now is:
function [value, isterminal, direction] = events(t,f)
value = 0;
isterminal = 1;
direction = 0;

options = odeset('Events',@events)
[t,f] = ode23('ODE3', [0 10],[x0,z0,px0,pz0],options);

The error message I get is:

??? SWITCH expression must be a scalar or string constant.

Error in ==> odeevents at 32
switch lower(eventFcn)

Error in ==> ode23 at 206
[haveEventFcn,eventFcn,eventArgs,valt,teout,yeout,ieout] = ...

Error in ==> untitled at 75
[t,f] = ode23('ODE3', [0 10],[x0,z0,px0,pz0],options);

where x0,z0,px0,pz0 are the given initial conditions. z0 is negative.


Thanks in advance


----------

