I would like to solve some equations with ode45.
The function is this:
function [ y_punto ] = bicicletta(t,y,a,b,u,delta,Ca,Cp,Iz,Fd)
v = y(1);
r = y(2);
alfaa = atan((v+a*r)/u)-delta;
alfap = atan((v-b*r)/u);
Fya = -Ca*alfaa;
Fyp = -Cp*alfap;
if (t>=tdon && t<=tdoff)
Fyad=Fd;
else
Fyad=0;
end
v_punto = 1/m*(Fya*cos(delta)+Fyp+Fyad)-ru;
r_punto = 1/Iz*(Fya*cos(delta)*a+Fyad*a-Fyp*b);
y_punto = [v_punto;r_punto];
end
Here is the script for starting simulation
clc
clear
a = 0.78;
b = 0.83;
Ca = 27537.5;
Cp = 27537.5;
Iz = 408.9;
u = 15*1000/3600;
delta = 5 * pi/180;
Fd = 100;
tf=5;
tspan = [0 tf];
y0 = [0;0];
[t,y] = ode45(bicicletta,tspan,y0);
The fact is that I need to pass extra parameters to the function. I don't know if I can define the function in this way
function [ y_punto ] = bicicletta(y)
and pass other parameters in another way for example declaring as global.
I did some search and I found this way of using ode45 but it gives me error
[t,y] = ode45(@(t,y)bicicletta(t,y,a,b,u,delta,Ca,Cp,Iz),tspan,y0);

2 comentarios

Stephen23
Stephen23 el 19 de Mzo. de 2016
Editada: Stephen23 el 19 de Mzo. de 2016
Your last line of code, where you create an anonymous function:
ode45(@(t,y)bicicletta(t,y,a,b,u,delta,Ca,Cp,Iz),tspan,y0);
is the correct approach to passing extra arguments. Please show us the complete error message (this means all of the red text).
Do not use globals: this is a slow and buggy way to write code.
Stephen23
Stephen23 el 21 de Mzo. de 2016
Editada: Stephen23 el 21 de Mzo. de 2016
SilverSurfer's "Answer" moved here:
Here his the error
Error in odearguments (line 87)
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 simulazione (line 18)
[t,y] = ode45(@(t,y)bicicletta(t,y,a,b,u,delta,Ca,Cp,Iz),tspan,y0);
I attached also the scripts

Iniciar sesión para comentar.

 Respuesta aceptada

Star Strider
Star Strider el 19 de Mzo. de 2016

1 voto

You have to define ‘tdon’ and ‘tdoff’ in your ‘bicicletta’ ODE function. I have no idea what you want to do, so I cannot correct that for you.

6 comentarios

SilverSurfer
SilverSurfer el 21 de Mzo. de 2016
Thanks, I didn't put them, anyway I still have the error. I attached the scripts.
Star Strider
Star Strider el 21 de Mzo. de 2016
The error your function call threw listed this line as being the problem:
[t,y] = ode45(@(t,y)bicicletta(t,y,a,b,u,delta,Ca,Cp,Iz),tspan,y0);
but the function you defined has one more argument, ‘Fd’:
function [ y_punto ] = bicicletta(t,y,a,b,u,delta,Ca,Cp,Iz,Fd)
You have to supply all them to your function.
SilverSurfer
SilverSurfer el 22 de Mzo. de 2016
Editada: SilverSurfer el 22 de Mzo. de 2016
I fixed the code. Now I supplied all the parameters used by the function. I'm still getting the error
Error in odearguments (line 87)
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 simulazione (line 20)
[t,y] = ode45(@(t,y)bicicletta(t,y,a,b,u,delta,Ca,Cp,Iz,Fd,tdon,tdoff),tspan,y0);
Here his the content of the latest script
a = 0.78;
b = 0.83;
Ca = 27537.5;
Cp = 27537.5;
Iz = 408.9;
u = 15*1000/3600;
delta = 5 * pi/180;
Fd = 100;
tdon=1;
tdoff=2;
tf=5;
tspan = [0 tf];
y0 = [0;0];
[t,y] = ode45(@(t,y)bicicletta(t,y,a,b,u,delta,Ca,Cp,Iz,Fd,tdon,tdoff),tspan,y0);
and function file.
function [ y_punto ] = bicicletta(t,y,a,b,u,delta,Ca,Cp,Iz,Fd,tdon,tdoff)
v = y(1);
r = y(2);
alfaa = atan((v+a*r)/u)-delta;
alfap = atan((v-b*r)/u);
Fya = -Ca*alfaa;
Fyp = -Cp*alfap;
if (t>=tdon && t<=tdoff)
Fyad=Fd;
else
Fyad=0;
end
v_punto = 1/m*(Fya*cos(delta)+Fyp+Fyad)-ru;
r_punto = 1/Iz*(Fya*cos(delta)*a+Fyad*a-Fyp*b);
y_punto = [v_punto;r_punto];
end
Torsten
Torsten el 22 de Mzo. de 2016
m and ru (maybe you mean r*u ?) in the definition of v_punto are undefined.
Best wishes
Torsten.
SilverSurfer
SilverSurfer el 22 de Mzo. de 2016
Editada: SilverSurfer el 22 de Mzo. de 2016
Thanks. I seems my code is full of little errors.
I missed also m in the script.
Now it runs.
Star Strider
Star Strider el 22 de Mzo. de 2016
‘Now it runs.’
Congratulations!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 19 de Mzo. de 2016

Comentada:

el 22 de Mzo. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by