How do I read this code?

1 visualización (últimos 30 días)
Douglas Alves
Douglas Alves el 12 de Mayo de 2014
Comentada: Star Strider el 12 de Mayo de 2014
Suppose I run this code by typing in the command line [t p] = ode45(@spring,[0 4], [0 0]); I don't understand first of all how MATLAB knows p is a vector and how does MATLAB know p is a 2x1 matrix ... ( It has to be a 2x1 matrix because I have 2 expressions in order to solve the system of differential equations). I think the answer comes from the initial values [0 0] but the initial value is not p!! p is unknown how come pdot is created...?
function pdot = spring(t,p,c,w)
pdot = zeros(size(p));
pdot(1) = p(2);
pdot(2) = sin(t) -c*p(2) - (w^2)*p(1);
  1 comentario
Douglas Alves
Douglas Alves el 12 de Mayo de 2014
This is another simple code that I still don't understand why it works like that. the variables are y1, y2 and y3. Why are they written as y(1), y(2), y(3) ?? y1 is different from y(1). Y(1) should be the first element of a vector Y... no??
function dy = f(t,y) dy = zeros(3,1); dy(1) = y(2)*y(3); dy(2) = -y(1)*y(3); dy(3) = -0.51*y(1)*y(2);

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 12 de Mayo de 2014
MATLAB doesn’t know that pdot is a (2x1) column vector until you tell it. That’s what you’re doing when you preallocate it with the zeros statement. Usually, you declare the value of p by preallocating it as ‘pdot = zeros(2,1);’, but since your initial conditions vector is [0 0], it preallocates correctly. The initial conditions vector is the first value for p because you just defined it as the initial condition (starting values) for p.
  3 comentarios
Douglas Alves
Douglas Alves el 12 de Mayo de 2014
Thank you very much Star_Strider. I totally got the part about pdot and initials conditions. But the second part about y(1) and y1... What I mean is y(1) should then be an scalar not a variable. Because Y is a vector and setting dy(1) = y(2)*y(3) [for instance] would put the product of two scalars y(2) and y(3) in the first position of the matrix dy. Whoever y(2) and y(3) are. no?? I think y(1) and y(2) should be something like variables x and z .. but what I think they really are is scalar numbers of a matrix y that I don't know what's in it.
I hope you understood my question..
Star Strider
Star Strider el 12 de Mayo de 2014
My pleasure.
As for coding various differential equations as a system of first-order equations (as all solvers require), I will direct you to a textbook on differential equations. They have a specific format that is dictated by the differential equations themselves. They have to return a vector of derivatives because the solutions to the differential equation function from the previous step are being returned to it as values of (in this instance) y(1) ... y(n). The differential equation function file then returns the derivatives evaluated at that time point and with those values of y(n). Your differential equation text should also have a section on Runge-Kutta numerical integration, to which this discussion applies.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by