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일

0 개 추천

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

I used the initial values as provided in the posted code.
Flipping the initial conditions vector and plotting the function and the derivative ...
tspan=[0 5];
y0=[0.02;0]
y0 = 2×1
0.0200 0
y0 = flip(y0)
y0 = 2×1
0 0.0200
% 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);
grid on
xlabel('‘time’')
ylabel('Displacement')
title('Displacement Vs Time')
hold on;
legend('y_1','y_2', 'Location','best')
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
... gives the same result.
The errors are in the original code, not my corrections to it (that allowed it to run without error). Neither of the integrated values (the displacement or velocity) are close to the desired plot.
In order to correct the error, I would need to see the differential equations that are being coded here.
.
Mert Dark
Mert Dark 2022년 1월 13일
This is the problem code and why my code does not same plot this article code?
Torsten
Torsten 2022년 1월 13일
Then either the plot in your assignment is wrong or the constants f,k,m,W,er and/or c used therein differ from yours.
Star Strider
Star Strider 2022년 1월 13일
@TorstenThank you!
@Mert Dark — The problem assignment statement is clearly misleading, because function files do not inherit any variables from the calling script, although an anonymous function version of ‘yp’ definitely would (if written in the code after the other variables were defined). That the other variables were not listed as arguments (as in my code) leads me to believe that whoever wrote the problem assignment is engaged in a significant amount of wishful thinking. It would be worthwhile to see the actual code that produced the plot (or plots) this code is supposed to produce. What are the other parameters? What values of the parameters produced the desired plot?
.
Mert Dark
Mert Dark 2022년 1월 14일
@Star Strider other parameters here photo1
Mert Dark
Mert Dark 2022년 1월 14일
@Star Strider other parameters here photo2
Mert Dark
Mert Dark 2022년 1월 14일
@Star Strider but i choose this paraeters just double all
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일

0 개 추천

but i need this graph

제품

릴리스

R2020b

질문:

2022년 1월 12일

댓글:

2022년 1월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by