Too many input arguments in ode45 using anonymous function

I am trying to solve a forced vibration problem using state spac representation. Please tell me what is wrong with my code.
mass = 750; % Mass of the body [kg]
stiffness = 50000; % Stiffness Coefficient of spring [N/m]
damping = 0; % Damping coefficient of damper [Ns/m]
time = 0:0.01:1; % Time [s]
x_0 = 0.01; % Initial Condition displacement
x_dot_0 = 0; % Initial Condition velocity
mass_extruded = 0.03; % Mass of the excitation [kg]
omega = 6.28; % Angular frequency of the excitation [1/s]
radius = 0.24; % Radius of the excitation [m]
force = omega^2*radius*mass_extruded; % Calculate the force with given parameters
w0 = [x_0, x_dot_0]; % Create a vector with initial conditions
A = [0 1; -stiffness/mass -damping/mass]; % Create system Matrix
B = [0; force/mass]; % Create excitation vector
dw = @(w) A*w - B*cos(omega*time); % Define derivative
[tsim,wsim] = ode45(@(w) dw, time, w0);

댓글 수: 3

madhan ravi
madhan ravi 2020년 6월 12일
편집: madhan ravi 2020년 6월 12일
@(t,w) ...
We can’t run picture , upload your code as text.
gives the same error
I have edited the question and added the text. Please kindly take a look

댓글을 달려면 로그인하십시오.

답변 (2개)

Steven Lord
Steven Lord 2020년 6월 12일

0 개 추천

The ODE solvers will generally (with the exception of ode15i) call your ODE function with two input arguments. [ode15i will call your ODE function with three input arguments.] Even if your ODE function doesn't use both of those input arguments, it must accept them.
Your dw function probably wants to accept the time input t and use it instead of the vector time that it currently uses.

댓글 수: 2

So what changes do I need to make?
As madhan ravi said, "dw = @(t, w) ...". My suspicion is that you want to use t instead of time in the body of the dw function.

댓글을 달려면 로그인하십시오.

Ameer Hamza
Ameer Hamza 2020년 6월 13일
There are some mistakes in the way you wrote the ODE and called ode45. Following code run fine
mass = 750; % Mass of the body [kg]
stiffness = 50000; % Stiffness Coefficient of spring [N/m]
damping = 0; % Damping coefficient of damper [Ns/m]
time = 0:0.01:1; % Time [s]
x_0 = 0.01; % Initial Condition displacement
x_dot_0 = 0; % Initial Condition velocity
mass_extruded = 0.03; % Mass of the excitation [kg]
omega = 6.28; % Angular frequency of the excitation [1/s]
radius = 0.24; % Radius of the excitation [m]
force = omega^2*radius*mass_extruded; % Calculate the force with given parameters
w0 = [x_0; x_dot_0]; % Create a vector with initial conditions
A = [0 1; -stiffness/mass -damping/mass]; % Create system Matrix
B = [0; force/mass]; % Create excitation vector
dw = @(t, w) A*w - B*cos(omega*t); % Define derivative
[tsim,wsim] = ode45(dw, time, w0); % equivalent: [tsim,wsim] = ode45(@(t, w) dw(t, w), time, w0);
plot(tsim, wsim)
legend({'x', 'x\_dot'})

카테고리

도움말 센터File Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

질문:

2020년 6월 12일

답변:

2020년 6월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by