what wrong in my code when i use ode 45,and what the error are mean

1 view (last 30 days)
function [dw,ds] = Mass_On_Stick_012345678(w,s,parameter)
%Parameter=[ b m R G g ]
b=parameter(1);m=parameter(2);R=parameter(3);G=parameter(4);g=parameter(5);
fi_1=w(1);
fi1_dot=w(2);
teta_1=s(1);
teta1_dot=s(2);
%%rt=R*[sin(teta_1)*cos(fi_1),sin(teta_1)*sin(fi_1),cos(teta_1)];
dw=[0;0];
dw(1)=fi1_dot;
dw(2)=(-fi1_dot)*((2*teta1_dot)/tan(teta_1)+(b/m));
ds=[0;0];
ds(1)=teta1_dot;
ds(2)=(g/R)*sin(teta_1)-((G*teta_1)/(m*(R^2)))+0.5*(fi1_dot^2)*sin(2*teta_1)-((b*teta1_dot)/m);
end
Not enough input arguments.
Error in Mass_On_Stick_012345678 (line 3)
b=parameter(1);m=parameter(2);R=parameter(3);G=parameter(4);g=parameter(5);
  2 Comments
Star Strider
Star Strider on 21 Jan 2020
First, your ODE function must return one vector, not two, so create one column vector from ‘dw’ and ‘ds’. (I leave that detail to you, however the original ‘w’ and ‘s’ values need to be a single column vector (I call it ‘ws’ here) and match the single column vector for the derivatives. The first argument also needs to be the independent variable (whatever variable the differential equations are differentiated with respect to, such as time).
Second, you need the ode45 call to it to be:
@(t,ws) Mass_On_Stick_012345678(t,ws,parameter)
with the ‘parameter’ vector existing in your workspace.
I am not listing this as an Answer because it is not one.
avrham malasa
avrham malasa on 21 Jan 2020
i try your suggestion but it stiil order me at the same error.
an what you mean that the first argument need to be independent variable?
function dw = Mass_On_Stick_012345678(t,w,parameter)
%Parameter=[ b m R G g ] ,w=[0;0;0;0];
b=parameter(1);m=parameter(2);R=parameter(3);G=parameter(4);g=parameter(5);
fi1=w(1);
fi1_dot=w(2);
teta1=w(3);
teta1_dot=w(4);
%rt=R*[sin(teta_1)*cos(fi_1),sin(teta_1)*sin(fi_1),cos(teta_1)];
dw=[0;0;0;0];
dw(1)=fi1_dot;
dw(2)=(-fi1_dot)*((2*teta1_dot)/tan(teta1)+(b/m));
dw(3)=teta1_dot;
dw(4)=(g/R)*sin(teta1)-((G*teta1)/(m*(R^2)))+0.5*(fi1_dot^2)*sin(2*teta1)-((b*teta1_dot)/m);
this the ode code.
clc;close all;
m=1;R=1.0;G=200;b=0;g=9.82;
Parameter=[ g ,m, R, G, b ];
opts = odeset('Reltol',1e-10,'AbsTol',3e-11,'Stats','on');
y0 = [pi/4 3 pi/2 0];
tSpan=linspace(0,3,2000);
[t,y]=ode45('Mass_On_Stick_012345678',tSpan,y0,opts,Parameter);
and thank you very much for you helping i appreciate that

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 21 Jan 2020
Try changing this
[t,y]=ode45('Mass_On_Stick_012345678',tSpan,y0,opts,Parameter);
to this
[t,y]=ode45(@(t,y)Mass_On_Stick_012345678(t,y,Parameter),tSpan,y0,opts);
  4 Comments
Walter Roberson
Walter Roberson on 21 Jan 2020
The syntax for passing extra parameters by listing them at the end of the call, has been considered obsolete for 20 years.
Unless you need to program in MATLAB 4 or earlier, you should forget about passing function names as quoted strings and should switch over to function handle form.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!