I am not getting a graph for my matlab program but I can run a program . I am attaching my file. can anyone help me with this?

1 次查看(过去 30 天)
clear all;
global m k c w_f F0
m=2;
k=2000;
c=10;
w_f=12;
F0=5;
dt=0.005;
t=0:dt:2;
y0=[0 0.5 0];
%ODE function
function dy=testode3_force(t,y)
f1=F0*sin(2*pi*w_f*t);
dy=[f1/m-k*y(2)/m-c*y(1)/m;y(1)];
[tsol,ysol]=ode45('testode3_force',t,y0);
damping_ratio=c/(2*sqrt(k*m));
plot(t,ysol(:,2))
figure
plot(t,ysol(:,1))
end

采纳的回答

Dyuman Joshi
Dyuman Joshi 2024-1-14
There's no need of initialize the variables as globals, so remove that line.
Also, the ode call needs to be outside the function. And pass the other parameters as inputs to the ODE function -
m=2;
k=2000;
c=10;
w_f=12;
F0=5;
dt=0.005;
t=0:dt:2;
y0=[0 0.5];
[tsol,ysol]=ode45(@(t,y) testode3_force(t,y,c,w_f,m,k,F0),t,y0);
damping_ratio=c/(2*sqrt(k*m));
plot(t,ysol(:,2))
figure
plot(t,ysol(:,1))
%ODE function
function dy=testode3_force(t,y,c,w_f,m,k,F0)
f1=F0*sin(2*pi*w_f*t);
dy=[f1/m-k*y(2)/m-c*y(1)/m;y(1)];
end

更多回答(1 个)

Anjaneyulu Bairi
Anjaneyulu Bairi 2024-1-14
Hi,
I understand that you are not able to plot the graph with the above code. The function "testcode3_force" has plot code and it is not getting called anywhere in the code.Make sure you call it necessary arguments then it will resolve your query.
Code for function calling :
testcode3_force(t,y0);
%assuming your second input is y0
I hope it helps to resolve your query.

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by