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?

3 views (last 30 days)
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

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 14 Jan 2024
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

More Answers (1)

Anjaneyulu Bairi
Anjaneyulu Bairi on 14 Jan 2024
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.

Tags

Community Treasure Hunt

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

Start Hunting!