Hello,
Here is a working simple script for solving an ODE.
clear all;
clc
close all
time_period = [0 10]; % tmin,tmax
initial = [0,10]; % initial cond [t,y]
[t,y]= ode45(@myode45function, time_period, initial);
figure(1)
plot(t,y(:,1))
axis([0 10 0 12])
title('ODE solution: y(t)=2-2*exp(-0.2*t) ','Fontweight','bold','fontsize',14);
function dy_dt = myode45function(t,y)
dy_dt=[0.4-y*0.2] % Output : dy_dt
end
Question :
If i want to solve an ODE which contain values as variables, how can i make the script work?
Like if i wanted to solve this : dy_dt=[0.4-y*0.2*b+d] , where for example b=2, d=3
If i define b=2, d=3 at the beginnihg of the script, and write
[t,y]= ode45(@myode45function, time_period, initial,b,d);
...
function dy_dt = myode45function(t,y,b,d)
It gives the error message like :
Not enough input arguments.
Error in Untitled3>myode45function (line 19)
dy_dt=[0.4-y*0.2*b] % Output : dy_dt
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 Untitled3 (line 9)
[t,y]= ode45(@myode45function, time_period, initial,b);
Can anyone help what is the proper way to do this? I want to give value to parameters (like b,d) at in the script, not in the myode45function
Thanks!

 採用された回答

Steven Lord
Steven Lord 2018 年 11 月 24 日

2 投票

See this documentation page linked from the ode45 documentation page.

1 件のコメント

madhan ravi
madhan ravi 2018 年 11 月 24 日
b=2;
d=3;
time_period = [0 10]; % tmin,tmax
initial = [0,10]; % initial cond [t,y]
[t,y]= ode45(@(t,y)myode45function(t,y,b,d), time_period, initial); %function call
figure(1)
plot(t,y(:,1))
axis([0 10 0 12])
title('ODE solution: y(t)=2-2*exp(-0.2*t) ','Fontweight','bold','fontsize',14);
function dy_dt = myode45function(t,y,b,d) %function definition
dy_dt=[0.4-y*0.2*b+d] % Output : dy_dt
end

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

その他の回答 (1 件)

カテゴリ

ヘルプ センター および File Exchange で Programming についてさらに検索

製品

リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by