Having trouble implementing a function

조회 수: 3 (최근 30일)
Chris
Chris 2013년 10월 22일
답변: sixwwwwww 2013년 10월 22일
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