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 Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by