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.
DAKSH GANATRA 17BME0726
DAKSH GANATRA 17BME0726 2020 年 6 月 12 日
gives the same error
DAKSH GANATRA 17BME0726
DAKSH GANATRA 17BME0726 2020 年 6 月 12 日
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 件のコメント

DAKSH GANATRA 17BME0726
DAKSH GANATRA 17BME0726 2020 年 6 月 12 日
So what changes do I need to make?
Steven Lord
Steven Lord 2020 年 6 月 12 日
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 日

0 投票

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 ExchangeNumerical 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