Too many input arguments in ode45 using anonymous function
Show older comments
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 Comments
madhan ravi
on 12 Jun 2020
Edited: madhan ravi
on 12 Jun 2020
@(t,w) ...
We can’t run picture , upload your code as text.
DAKSH GANATRA 17BME0726
on 12 Jun 2020
DAKSH GANATRA 17BME0726
on 12 Jun 2020
Answers (2)
Steven Lord
on 12 Jun 2020
0 votes
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 Comments
DAKSH GANATRA 17BME0726
on 12 Jun 2020
Steven Lord
on 12 Jun 2020
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
on 13 Jun 2020
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'})

Categories
Find more on Numerical Integration and Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!