From: Marco Letizia on
Hi, i need to solve this differential equation with matlab:

mtest*g*r1-k_r*r1-k_v*r1*y'+massatot*g*d*cos(y)=(Itot+(mtest*r1^2))*y"

i tried to solve it with the following lines:

>>
function z=eq_diff(t,y)

load variabili

g=9.81;

z=[y(2);(mtest*g*r1-k_r*r1-k_v*r1*y(2)+massatot*g*d*cos(y(1)))/(Itot+(mtest*r1^2))];

>>

but matlab doesn't give me any response or error.. can you help me? what's wrong with what i wrote?
From: John D'Errico on
"Marco Letizia" <letissier85(a)hotmail.com> wrote in message <hr96qr$hpr$1(a)fred.mathworks.com>...
> Hi, i need to solve this differential equation with matlab:
>
> mtest*g*r1-k_r*r1-k_v*r1*y'+massatot*g*d*cos(y)=(Itot+(mtest*r1^2))*y"
>
> i tried to solve it with the following lines:
>
> >>
> function z=eq_diff(t,y)
>
> load variabili
>
> g=9.81;
>
> z=[y(2);(mtest*g*r1-k_r*r1-k_v*r1*y(2)+massatot*g*d*cos(y(1)))/(Itot+(mtest*r1^2))];
>
> >>
>
> but matlab doesn't give me any response or error.. can you help me? what's wrong with what i wrote?

Did you actually use ode45? How did you call it? Did
you expect it to work magically simply by writing a
function? Hardly.

Did you save this function as an m-file? Or did you
just type it into the command line, and hope something
would work? Did you get any errors? Tell us.

Next, lets look at your function. DON'T put a load
statement inside your function. This will simply eat
up a lot of time. Better is to pass in the parameters
that you would have loaded with the load. Use an
anonymous function for this. (DON'T use globals
for this. They are simply not necessary here.)

Even better is to never need to write and save a
function m-file at all. Create a function handle that
does what you need.

eq_diff = @(t,y) [y(2); (mtest*g*r1-k_r*r1-k_v*r1*y(2)+ ...
massatot*g*d*cos(y(1)))/(Itot+(mtest*r1^2))];

Finally, call ode45 with this function. I'll let you do
that part.

John
From: Gang-Gyoo on
"Marco Letizia" <letissier85(a)hotmail.com> wrote in message <hr96qr$hpr$1(a)fred.mathworks.com>...
> Hi, i need to solve this differential equation with matlab:
>
> mtest*g*r1-k_r*r1-k_v*r1*y'+massatot*g*d*cos(y)=(Itot+(mtest*r1^2))*y"
>
> i tried to solve it with the following lines:
>
> >>
> function z=eq_diff(t,y)
>
> load variabili
>
> g=9.81;
>
> z=[y(2);(mtest*g*r1-k_r*r1-k_v*r1*y(2)+massatot*g*d*cos(y(1)))/(Itot+(mtest*r1^2))];
>
> >>
>
> but matlab doesn't give me any response or error.. can you help me? what's wrong with what i wrote?

The following RK4 program will give what you want.
dont't use 'load variabili'. Instead you should define your parameters in 'eq_diff'.

function main
t= 0; y= [0;0]; h= 0.01; loop=100;
buf=[t y'];
for i= 1:loop
y= RK4(@eq_diff, t, y, h);
t= t+h;
buf= [buf; t y']
plot(buf(:,1), buf(:,2))
axis([0 h*loop 0 5])
pause(0.01)
end

function xnew= RK4(fun, t, x, h)
k1 = h*fun(t,x);
k2 = h*fun(t+0.5*h,x+0.5*k1);
k3 = h*fun(t+0.5*h,x+0.5*k2);
k4 = h*fun(t+h,x+k3);
xnew= x+(k1+2*(k2+k3)+k4)/6;

function z= eq_diff(t,y)
mtest= 1; r1= 1; massatot= 4; Itot= 1; k_v= 2; d=1;k_r= 3;
g= 9.81;
z(1)= y(2);
z(2)= (mtest*g*r1-k_r*r1-k_v*r1*y(2)+massatot*g*d*cos(y(1)))/(Itot+(mtest*r1^2));
z= z';
From: Marco Letizia on
"Gang-Gyoo " <ggjin(a)hhu.ac.kr> wrote in message <hr9dvc$ac1$1(a)fred.mathworks.com>...
> "Marco Letizia" <letissier85(a)hotmail.com> wrote in message <hr96qr$hpr$1(a)fred.mathworks.com>...
> > Hi, i need to solve this differential equation with matlab:
> >
> > mtest*g*r1-k_r*r1-k_v*r1*y'+massatot*g*d*cos(y)=(Itot+(mtest*r1^2))*y"
> >
> > i tried to solve it with the following lines:
> >
> > >>
> > function z=eq_diff(t,y)
> >
> > load variabili
> >
> > g=9.81;
> >
> > z=[y(2);(mtest*g*r1-k_r*r1-k_v*r1*y(2)+massatot*g*d*cos(y(1)))/(Itot+(mtest*r1^2))];
> >
> > >>
> >
> > but matlab doesn't give me any response or error.. can you help me? what's wrong with what i wrote?
>
> The following RK4 program will give what you want.
> dont't use 'load variabili'. Instead you should define your parameters in 'eq_diff'.
>
> function main
> t= 0; y= [0;0]; h= 0.01; loop=100;
> buf=[t y'];
> for i= 1:loop
> y= RK4(@eq_diff, t, y, h);
> t= t+h;
> buf= [buf; t y']
> plot(buf(:,1), buf(:,2))
> axis([0 h*loop 0 5])
> pause(0.01)
> end
>
> function xnew= RK4(fun, t, x, h)
> k1 = h*fun(t,x);
> k2 = h*fun(t+0.5*h,x+0.5*k1);
> k3 = h*fun(t+0.5*h,x+0.5*k2);
> k4 = h*fun(t+h,x+k3);
> xnew= x+(k1+2*(k2+k3)+k4)/6;
>
> function z= eq_diff(t,y)
> mtest= 1; r1= 1; massatot= 4; Itot= 1; k_v= 2; d=1;k_r= 3;
> g= 9.81;
> z(1)= y(2);
> z(2)= (mtest*g*r1-k_r*r1-k_v*r1*y(2)+massatot*g*d*cos(y(1)))/(Itot+(mtest*r1^2));
> z= z';

the problem is:

i used the command "load variabili" because the script in which i need to solve the differential equation accept as input those variables, and i need them into the .m file of the function.
Obviously the function i posted is in a .m file and i run this function calling it into the terminal with :

tstan=[0 t_srotolamento];

y0=[0 0];

[t,y]=ode45('eq_diff',tstan,y0);

t

y

I have to say that the problem occourred when i added the massatot*g*d*cos(y1) part. Without that part, i had correct plots and outputs from the function, that's why i was wondering if i did some mistake writing the last part of the function.
From: John D'Errico on
"Marco Letizia" <letissier85(a)hotmail.com> wrote in message <hr9etc$cke$1(a)fred.mathworks.com>...

> the problem is:
>
> i used the command "load variabili" because the script in which i need to solve the differential equation accept as input those variables, and i need them into the .m file of the function.

NO. You don't need to load it. READ MY response.

When you create the function handle, those variables
are available to the function to use. They are passed
in properly. You will find the difference is significant,
by avoiding those wasted loads.


> Obviously the function i posted is in a .m file and i run this function calling it into the terminal with :
>
> tstan=[0 t_srotolamento];
>
> y0=[0 0];
>
> [t,y]=ode45('eq_diff',tstan,y0);
>
> t
>
> y
>
> I have to say that the problem occourred when i added the massatot*g*d*cos(y1) part. Without that part, i had correct plots and outputs from the function, that's why i was wondering if i did some mistake writing the last part of the function.

If you are have a term in it that reads like this...

massatot*g*d*cos(y1)

then I would expect an error. Matlab does not
know what y1 is. If this was a typo on your part
in this post, then show us what the error was.

John