Having trouble implementing a function

1 Ansicht (letzte 30 Tage)
Chris
Chris am 22 Okt. 2013
Beantwortet: sixwwwwww am 22 Okt. 2013
I have this function following eulers theorem and its all nicely labeled but i cant make it work because it keeps saying unknown operator. it says to put the function as an inline function and then graph euler.m i am very lost as to how to make this work.
in one file i have
f=inline('-2*y')
and in another file i have eulers
function [t,y] = euler(f,tspan,y0,N);
% Solves the IVP y' = f(t,y), y(t0) = y0 in the time interval tspan = [t0,tf]
% using Euler's method with N time steps.
% Input:
% f = name of inline function or function M-file that evaluates the ODE
% (if not an inline function, use: euler(@f,tspan,y0,N))
% For a system, the f must be given as column vector.
% tspan = [t0, tf] where t0 = initial time value and tf = final time value
% y0 = initial value of the dependent variable. If solving a system,
% initial conditions must be given as a vector.
% N = number of steps used.
% Output:
% t = vector of time values where the solution was computed
% y = vector of computed solution values.
m = length(-30);
t0 = 0;
tf = 10;
h = (tf-t0)/N; % evaluate the time step size
t = linspace(t0,tf,N+1); % create the vector of t values
y = zeros(m,N+1); % allocate memory for the output y
y(:,1) = y0'; % set initial condition
for n=1:N
y(:,n+1) = y(:,n) + h*f(t(n),y(:,n)); % implement Euler's method
end
t = t'; y = y'; % change t and y from row to column vectors
end
I keep trying to input -2*y for my f and [0,10] for my tspan and all the other constants in the function but it is not working.
  1 Kommentar
Chris
Chris am 22 Okt. 2013
update. I think i almost have it figured out. i have them both on the same file defining and inline function and the i just called eulers but now its telling me not enough inputs for my inline function. what does it mean?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

sixwwwwww
sixwwwwww am 22 Okt. 2013
It means you should input all four values for "f,tspan,y0,N" because your "euler" function has four inputs

Weitere Antworten (0)

Kategorien

Mehr zu Function Creation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by