How can l solve the system with multiple variables using ode45? For example l want to solve the following system for variables x1 x2 y1 y2 with respect to time t:
x1'=x2/4 - (5*x1)/16 + (15*y1)/16 - (3*y2)/4 - x1*(x1^2 + y1^2 - 1) - 3*y1*(x1^2 + y1^2)
y1'= (3*x2)/4 - (15*x1)/16 - (5*y1)/16 + y2/4 + 3*x1*(x1^2 + y1^2) - y1*(x1^2 + y1^2 - 1)
x2'=x1/8 - (5*x2)/16 + x3/8 - (3*y1)/8 + (15*y2)/16 - (3*y3)/8 - x2*(x2^2 + y2^2 - 1) - 3*y2*(x2^2 + y2^2)
y2'=(3*x1)/8 - (15*x2)/16 + (3*x3)/8 + y1/8 - (5*y2)/16 + y3/8 + 3*x2*(x2^2 + y2^2) - y2*(x2^2 + y2^2 - 1)
My question is that is there a simple way to repersent the variables x1 y1 x2 y2 when use ode45 to solve the equations in time rather than defining each variable as A(1)=x1,A(2)=y1A(3)=x2,A(4)=y2
Is there a approach that allows me to:
f=@(t,x1,y1,x2,y2) ...............
[t,R]=(f,tspan,initial condition);
So that i will not need to repersent each variable as the elements in the matrix A. Please help me, thank you very much.

 채택된 답변

madhan ravi
madhan ravi 2020년 6월 10일

1 개 추천

Below is the right way to do , but maybe you're looking for matlabFunction()?
Y = zeros(4,1);
Y(1)=x1
Y(2)=y1
Y(3)=x2
Y(4)=y2

댓글 수: 4

Penglin Cai
Penglin Cai 2020년 6월 10일
can l put x1 y1 x2 y2 directly in the ode45 to solve them with respect to time?
madhan ravi
madhan ravi 2020년 6월 10일
No
If you definitely want to use the variable x1 y1 x2 y2 in your ODE-function (there are often decent enough reasons for this in terms of readability), you simply do something like this up top in your ODE-function:
function dAdt = yourODEfcn(t,A)
x1 = A(1);
x2 = A(2);
y1 = A(3);
y2 = A(4);
% etc
% Then remaining code here
end
madhan ravi
madhan ravi 2020년 6월 10일
Thank you Bjorn was thinking to write in that way but somehow messed it up xD.

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

추가 답변 (0개)

카테고리

제품

릴리스

R2019b

태그

질문:

2020년 6월 10일

댓글:

2020년 6월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by