필터 지우기
필터 지우기

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