Solving differential equation using ode45 with three variables

조회 수: 6 (최근 30일)
Jay Kim
Jay Kim 2018년 8월 21일
답변: goutham baburaj 2019년 7월 9일
Hi I'm trying to solve a simple second-order differential equation y''-2y'+y=0, with initial conditions where y'=0 and y=0.
I have successfully made a function of
function dydt=ode5( t, y)
dydt=zeros(2,1);
dydt(1)=y(2);
dydt(2)=2*y(2)-y(1);
end
but I want to make a function that can substitute dydt=p or some other symbol to simply put it as
function dydx=ode9(x,p,y)
p=zeros(2,1);
dydx=p;
dpdx=-p+2*y;
end
but I keep getting errors. Help!
  댓글 수: 7
Torsten
Torsten 2018년 8월 21일
The only meaningful I can think of is
[X,Y]=ode45(@(x,y)ode9(x,y(1),y(2)),tspan,y0)
function derivatives = ode9(x,y,yp)
derivatives = zeros(2,1)
dydx = yp;
dypdx = 2*yp-y;
derivatives = [dydx;dypdx]
end
Walter Roberson
Walter Roberson 2018년 8월 21일
Do I understand correctly that you would like to pass in as p some indication of which y entry to use?
Would the code always be as simple as dydx being assigned y indexed at some fixed value?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2018년 8월 21일
p = 1;
[X,Y]=ode45(@(x,y)ode9(x, p, y), tspan, y0);
function derivatives = ode9(x, p, y)
derivatives = zeros(2,1);
derivatives(1) = y(p);
derivatives(2) = 2*y(p)- y(3-p);
end
Here I had to guess what you wanted. You subtract y for the second of the two derivatives, but y is a vector of two elements. I had to guess that you wanted the other y entry, the one not selected by p. When you have a p that is either 1 or 2, then 3-p would be 2 or 1, thereby selecting the other entry.

추가 답변 (1개)

goutham baburaj
goutham baburaj 2019년 7월 9일
thanks

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by