Please help me understand the ode45 usage

119 Views Asked by At

I'm studying numerical analysis and the answer for this problem (the question is in English) that is solved with the following code:

p1 = -20.2090;
p2 = 17.3368;
p3 = 272.9057;
beta = 7.8*pi/180;
x0 = 2000;

c = @(z)4800+p1+p2*z(1)/1000+p3*exp(-.75*z(1)/1000);
cp = @(z)(p2/1000-.75/1000*p3*exp(-.75*z(1)/1000));
q0 = (c(x0)/cos(beta))^2;

ode = @(t,z)[z(2); -q0*cp(z)/c(z).^3];
IC = [x0; tan(beta)];
[x,z] = ode45(ode,[0 25*6076], IC); 
plot(x,z(:,1))

But I don't understand the strategy of the above code. I could think that a second-degree differential equation should be solved using a transformation from second degree to a system of first-degree equation but it doesn't look like that is done. I do understand how we get the coefficients p1, p2, p3, beta and x0, that the c=... is the speed of sound related to depth and cp=... is the derivative. I would like to know what happens next and why the code solves the equation when it doesn't do a transformation. What is the purpose and meaning of z(2)in the ode and why does the ode look like is does? I know that ; in matlab means a second row and that z(2) means the second element in z, but why are those uder and how does it contribute to the correct answer?

Please help me understand.

1

There are 1 best solutions below

0
On BEST ANSWER

Copying my previous comment:

The vector $z$ in your code has two components, which represent the value of the solution (call it $y$ for clarity) and the derivative of the solution, $y'$. Your ODE function says to use $y'$ (the second component) as the derivative of $y$, and to use the equation you were given for $y''$ to furnish the derivative of $y'$. You will always proceed this way when time-stepping an ODE of order higher than 1, at least when using a "black box" function like ode45.