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?
2 ビュー (過去 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
0 件のコメント
採用された回答
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
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 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!