Using ODE45 to solve quarter model of suspension with input function (an endless obstacle)
15 views (last 30 days)
Show older comments
Hi, i tried solve a quarter model of suspension. I want input function, there function is road (an endless obstacle)
I tried solve differential equation, but i can´t input the obstacle.
t=0:0.01:3;
u1(t<=0.25)=0;
u1(t>0.25)=0.077;
U=zeros(4,301);
U(3,:)=u1;
[t,y]=ode45(@(t,y) fce(t,y,U),t,[0 0 0 0]);
with funciton
function dz=fce(t,z,u)
global b k10 k12 m1 m2
dz=zeros(4,1);
dz(1) = z(3);
dz(2) = z(4);
dz(3) = -((k10+k12)/m1)*z(1)+k12/m1*z(2)-b/m1*z(3)+b/m1*z(4);+k10/m1*u(3);
dz(4) = k12/m2*z(1)-k12/m2*z(2)+b/m2*z(3)-b/m2*z(4);
The program works, but the solution is matrix zero (so the program not working with input function (U)). Can you help me to solve this problem?
0 Comments
Answers (1)
Torsten
on 12 Nov 2018
Edited: Torsten
on 12 Nov 2018
t = 0:0.01:0.25;
phase = 1;
y0 = [0 0 0 0];
[t1, y1] = ode45(@(t,y) fce(t,y,phase),t,y0);
t = 0.25:0.01:3;
phase = 2;
y0 = y1(end,:);
[t2, y2] = ode45(@(t,y) fce(t,y,phase),t,y0);
T = [t1; t2];
Y = [y1; y2];
plot(T,Y)
function dz=fce(t,z,phase)
global b k10 k12 m1 m2
if phase == 1
u = 0;
else
u = 0.077;
end
dz = zeros(4,1);
dz(1) = z(3);
dz(2) = z(4);
dz(3) = -((k10+k12)/m1)*z(1)+k12/m1*z(2)-b/m1*z(3)+b/m1*z(4);+k10/m1*u;
dz(4) = k12/m2*z(1)-k12/m2*z(2)+b/m2*z(3)-b/m2*z(4);
2 Comments
faruk özalp
on 11 Jan 2022
this code gave this error
All functions in a script must be closed with an 'end'.
Torsten
on 11 Jan 2022
Edited: Torsten
on 11 Jan 2022
Then do what MATLAB tells you to do:
function main
t = 0:0.01:0.25;
phase = 1;
y0 = [0 0 0 0];
[t1, y1] = ode45(@(t,y) fce(t,y,phase),t,y0);
t = 0.25:0.01:3;
phase = 2;
y0 = y1(end,:);
[t2, y2] = ode45(@(t,y) fce(t,y,phase),t,y0);
T = [t1; t2];
Y = [y1; y2];
plot(T,Y)
end
function dz=fce(t,z,phase)
global b k10 k12 m1 m2
if phase == 1
u = 0;
else
u = 0.077;
end
dz = zeros(4,1);
dz(1) = z(3);
dz(2) = z(4);
dz(3) = -((k10+k12)/m1)*z(1)+k12/m1*z(2)-b/m1*z(3)+b/m1*z(4);+k10/m1*u;
dz(4) = k12/m2*z(1)-k12/m2*z(2)+b/m2*z(3)-b/m2*z(4);
end
But don't forget to give values to the missing variables b, k10, k12, m1 and m2.
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!