Having trouble implementing a function

3 次查看(过去 30 天)
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 个评论
Chris
Chris 2013-10-22
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?

请先登录,再进行评论。

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by