ode45 inside calling funciton error

1 次查看(过去 30 天)
I have forced.m funciton and i want to use inside ode45 funciton but doesn't work i have error
Not enough input arguments.
Error in forced (line 2)
yp = [y(2);(((f/m)*sin(W*t))-((c/m)*y(2))-((k/m)*y(1)))];
Error in Untitled2 (line 11)
[t,y]=ode45(forced, tspan, y0);
main code
tspan=[0 5];
y0=[0.02;0];
x=y(:,1);
f=200;
k=200;
m=2;
W=sqrt(k/m);
er=0.1;
c=2*er*W*m;
[t,y]=ode45(forced, tspan, y0);
plot(t,y(:,1)); grid on xlabel(‘time’)
ylabel('Displacement')
title('Displacement Vs Time')
hold on;
forced.m
function yp = forced(t,y)
yp = [y(2);(((f/m)*sin(W*t))-((c/m)*y(2))-((k/m)*y(1)))];

回答(2 个)

Star Strider
Star Strider 2022-1-12
All the arguments the function needs must be passed to it as additional parameters.
See the documentation section on Passing Extra Parameters.
tspan=[0 5];
y0=[0.02;0];
% x=y(:,1);
f=200;
k=200;
m=2;
W=sqrt(k/m);
er=0.1;
c=2*er*W*m;
[t,y]=ode45(@(t,y)forced(t,y,f,k,m,W,er,c), tspan, y0);
plot(t,y(:,1));
grid on
xlabel('‘time’')
ylabel('Displacement')
title('Displacement Vs Time')
hold on;
function yp = forced(t,y,f,k,m,W,er,c)
yp = [y(2);(((f/m)*sin(W*t))-((c/m)*y(2))-((k/m)*y(1)))];
end
.
  9 个评论
Torsten
Torsten 2022-1-14
编辑:Torsten 2022-1-14
W = 2*sqrt(k/m)
instead of
W = sqrt(k/m)
and consequently
c = er*W*m
instead of
c = 2*er*W*m
in your code.
Star Strider
Star Strider 2022-1-14
@Torsten — Thank you!
Away for a few minutes here.

请先登录,再进行评论。


Mert Dark
Mert Dark 2022-1-13
but i need this graph

类别

Help CenterFile Exchange 中查找有关 Multirate Signal Processing 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by