Why am i getting error too many input arguments ?

2 Ansichten (letzte 30 Tage)
Adrian Pamungkas
Adrian Pamungkas am 20 Mai 2018
Bearbeitet: Stephen23 am 20 Mai 2018
I have a problem to solve an ODE with any methods. The question was The motion of a damped spring-mass system is described by the following ordinary differential equation:
m fraction numerator d squared x over denominator d t squared end fraction plus c fraction numerator d x over denominator d t end fraction plus k x equals 0
where x = displacement from equilibrium position, t = time, m = 20 kg is the mass, k = 20 N/m is the spring constant, c = the damping coefficient. The damping coefficient c takes on three values of 5 (underdamped), 40 (critically damped), and 200 (overdamped). The initial velocity is zero, and the initial displacement x = 1 m.
Develop an M-file to solve this equation using a numerical method (choose any method you prefer most) over the time period 0 less or equal than t less or equal than 15 space s. Plot the displacement versus time for each of the three values of the damping coefficient on the same plot."
Then I make my 3 codes , first one is the euler methods
function [t,y] = eulode (dydteulode,tspan,y0,h)
%eulode : EULER ODE SOLVER
% [t,y] = eulode (dydt,tspan,y0,h,p1,p2,...) :
% use Euler method to integrate ODE
%input:
%dydt = name of the M-file that evaluates ODE
%tspan = [ti, tf] where ti and tf = initial and final values of
%independent variable
%y0= initial value of dependent variable
%h= step size
%p1,p2,...= additional parameters used by dydt
%output:
% t= vector of independent variable
% y= vector of solution for dependent variable
ti= tspan(1);tf = tspan(2);
t = (ti:h:tf)';
n = length(t);
%if necessary, add an additional value of t
% so that range goes from t = ti to tf
if t(n)<tf
t(n+1) = tf;
n = n+1;
end
y(1,:)= y0; %preallocate y to improve accuracy
for i = 1:n-1 %implement Euler's method
hh = t(i+1)-t(i);
k = feval(dydteulode,t(i),y(i,:))';
y(i+1,:) = y(i,:)+k*hh;
end
then the function for the graphic plot
function []=graphploteulode()
clear,clc,clf
k= 20; m=20; tspan= [0:1/4:15]; y0= [1 0];
%defiining constants and initial condition given in the problem
%tspan i.e. time is incremented with step size 1/4
[t1,y1]= eulode(@dydteulode,tspan,y0,1/4,m,k,5);
%integrating for underdamped condition
[t2,y2]= eulode(@dydteulode,tspan,y0,1/4,m,k,40);
%integrating for critically damped condition
[t3,y3]= eulode(@dydteulode,tspan,y0,1/4,m,k,200);
%integrating for overdamped condition
plot (t1,y1(:,1),t2,y2(:,1),':',t3,y3(:,1),'--')
legend ('underdamped','critically damped','overdamped','location','best')
title ('Displacements for mass-spring system')
xlabel('t(s)'),ylabel('x(m)')
%generating required plot
end
and last the equation itself
function dy = dydteulode (t,y,m,k,c)
dy= [y(2);-(c*y(2)+k*y(1))/m];
end
but when I run the graphplot i got an error which is to many input arguments and it points to my code on line6 in graphplot which is [t1,y1]= eulode(@dydteulode,tspan,y0,1/4,m,k,5);
I don't know why is this happened, please help thank you.

Akzeptierte Antwort

Stephen23
Stephen23 am 20 Mai 2018
Bearbeitet: Stephen23 am 20 Mai 2018
Here is the function defintion of eulode (including superfluous space between function name and parenthesis):
function [t,y] = eulode (dydteulode,tspan,y0,h)
When I count the number of input argument there are four of them. This is how you call eulode:
[t1,y1]= eulode(@dydteulode,tspan,y0,1/4,m,k,5);
When I could the input arguments there are seven of them. You defined the function with four input arguments, and then call it with seven inputs. This is an error. To pass extra arguments to any function you could use nested functions or define an anonymous function, exactly as the MATLAB documentation shows:

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 20 Mai 2018

Community Treasure Hunt

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

Start Hunting!

Translated by