Having trouble implementing a function
Afficher commentaires plus anciens
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 commentaire
Chris
le 22 Oct 2013
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Function Creation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!