ODE45 function returning error for a system of differential equations

Hello MATLABers,
I am trying to run this code:
if true
clc
clear
close all
format long
odefun = @deriv ;
tspan = [0 20] ;
Y0 = [ 10; 10; 10; 10; 10 ] ;
[t,y] = ode45( odefun, tspan, Y0, [] ) ;
function dYdt = deriv(Y)
Y1 = Y(1) ;
Y2 = Y(2) ;
Y3 = Y(3) ;
Y4 = Y(4) ;
Y5 = Y(5) ;
dYdt = zeros(5,1) ;
dYdt(1) = ( 250 - (7*Y1) + (2*Y3) ) / 100 ;
dYdt(2) = ( (4*Y1) - (4*Y2) ) / 100 ;
dYdt(3) = ( 400 + Y2 - (9*Y3) ) / 100 ;
dYdt(4) = ( Y2 + (7*Y3) - (11*Y4) + (3*Y5) ) / 100 ;
dYdt(5) = ( (3*Y1) + (2*Y2) - (5*Y5) ) / 100 ;
end
But I keep getting the following error:
if true
---> Error using ODE45_Trial>deriv
Too many input arguments.
---> Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
---> Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
---> Error in ODE45_Trial (line 12)
[t,y] = ode45( odefun, tspan, Y0, [] ) ;
I have no idea, what I am doing wrong, because I checked several webpages and read through many questions on the MATLAB community, but was not able to arrive at a solution. Please help me, this is due tomorrow. Any help is appreciated.
P.S the 'if true' at the start is not a part of the code. Thank You

 採用された回答

Torsten
Torsten 2018 年 2 月 5 日

0 投票

Use
[t,y] = ode45( odefun, tspan, Y0 ) ;
function dYdt = deriv(t,Y)
Best wishes
Torsten.

3 件のコメント

Deep Sheth
Deep Sheth 2018 年 2 月 5 日
Yes I did try that, but there are no changes at all. I added the [] because one of the posts somewhere suggested to add an empty options thing or something.
Torsten
Torsten 2018 年 2 月 5 日
You added the t-variable in the input-list of "deriv" ?
Deep Sheth
Deep Sheth 2018 年 2 月 5 日
That worked. Thanks a lot. It is amazing how silly things can lead to great tensions.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

2018 年 2 月 5 日

コメント済み:

2018 年 2 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by