ode45 2nd order IVP **please help!!**

The problem I am trying to solve is this:
"Consider the IVP z"+4z'+3z=e^x, z(0)=1 and z'(0)=2. Rewrite it as a system represented by ex3_2ndorder.m. Be sure to put comments after the signature line and wherever else they are needed. Be sure you return a column vector. Use ode45 to solve for z up to x=4."
I am fairly new to MATLAB but the code I wrote is this:
function yprime = ex3_2ndorder(x,y)
%name and date
yprime=zeros(2,1);
yprime(1)=y(2);
yprime(2)=exp(x)-4*y(2)-3*y(1);
end
However, when I try to run it, I get an error message saying "Not enough input arguments. Error in line 4, yprime(1)=y(2);"
Could someone please tell me if my code is correct and why I am receiving this error? Thanks!!

 채택된 답변

darova
darova 2019년 10월 18일

3 개 추천

You are almost there! Yyou written function for ode45 but you didn't use it!
function yprime = ex3_2ndorder(x,y)
%name and date
yprime=zeros(2,1);
yprime(1)=y(2);
yprime(2)=exp(x)-4*y(2)-3*y(1);
end
Create new script (new script) and type
[t,y] = ode45(@ex3_2ndorder,[0 4],[0 2]);
plot(t,y)
See more HERE

댓글 수: 4

James Tursa
James Tursa 2019년 10월 18일
편집: James Tursa 2019년 10월 18일
Last argument for ode45( ) call should be [1,2]
darova
darova 2019년 10월 18일
YES!
nb1
nb1 2019년 10월 18일
Thank you so much! Should the part I didn't include be included in my original code?
Also, I'm still getting the same error message when I try to run it :( Is yprime(1)=y(2) incorrect? or do I need to add something to that line to give it more input arguments?
Is yprime(1)=y(2) incorrect?
Yes, it is ok
You should run the main script and the function should be saved as a separate file (named exactly as ex3_2ndorder)
OR you can write it in only one script!
F = @(x,y) [y(2); exp(x)-4*y(2)-3*y(1)];
[t,y] = ode45(F,[0 4],[1 2]);
plot(t,y)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

릴리스

R2019b

질문:

nb1
2019년 10월 18일

편집:

nb1
2019년 10월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by