필터 지우기
필터 지우기

How to write ode45 function correctly

조회 수: 1 (최근 30일)
Teb Keb
Teb Keb 2020년 10월 10일
답변: Ameer Hamza 2020년 10월 10일
I am trying to solve 2nd order ODE using matlab and I am having a problem with my function file.
My original equation is as follows:
I wrote my 2nd order in terms of 1st order and have 2 sets of equation as the following:
x1=x
x2=dx
And my 1st order ode’s are:
dx=x2
dxdt=b1*x1-a*x2+b2*x1^4+4t
The function I wrote in matlab is:
function dxdt= model(t,x,m,a,b1,b2)
x=x(1);
dx=x(2);
dxdt(1)=dx;
dxdt(2)=b1*x-a*dx+b2*x^4+4*t;
end
But matlab kept saying there is an error at x=x(1). I know x1 and x2 are vectors but I am not sure how to write it in this function.

답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 10월 10일
You are oberwriting variable 'x' inside model(). Try this
m = 1;
a = 0.5;
b1 = 0.1;
b2 = 0.2;
tspan = [0 10];
ic = [1; 0];
ode45(@(t,x) model(t,x,m,a,b1,b2), tspan, ic)
function dxdt= model(t,x,m,a,b1,b2)
x_=x(1);
dx=x(2);
dxdt(1)=dx;
dxdt(2)=b1*x_-a*dx+b2*x_^4+4*t;
dxdt = dxdt(:); % it must return a column matrix
end

카테고리

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

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by