I should get a straight line graph in this equation after substituting initial conditions. I dont know where I am getting wrong. Can anyone help me with this?
1 次查看(过去 30 天)
显示 更早的评论
tspan=[0,1];
z0=[0,0.01,0,0]; %initial position and velocity
%[x0,vx0,y0,vy0]
%solve the ODEs
[t, z]=ode45(@rhs,tspan,z0);
% Unpack the variables
x1=z(:,1);
v1=z(:,2);
x2=z(:,3);
v2=z(:,4);
%Plot the results
plot(t,v1);
title(' x1 vs t');
xlabel('t(s)');
ylabel('x1(m/s)');
figure
plot(t,v2);
title(' x2 vs t');
xlabel('t(s)');
ylabel('x2(m/s)');
%set grid,xmin,xmax,ymin,ymax
%___________________
function zdot=rhs(t, z)
x1=z(1); v1=z(2); x2=z(3); v2=z(4);
%put in values for mass ,C and g below
m=1;
k=1;
c=0.001;
h=0.01;
% the linear momentum balance equation
x1dot=v1;
v1dot=0;
x2dot=v2;
v2dot=(k/m)*(x1-x2)-(c/h*m)*v2;
zdot=[x1dot;v1dot;x2dot;v2dot];
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1595141/image.jpeg)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!