Vous êtes sur la page 1sur 7

P

*No precise definition of stiffness, but the main idea is that the equation
includes some terms that can lead to rapid variation in the solution.
Defining an ODE function in an M-file

 [t,state] = ode45(@dstate,tspan,ICs,options)
 1. Define tspan, ICs and options in one file (e.g. call_dstate.m), which sets
up ode45
 2. Define your constants and derivatives in another file (e.g. dstate.m) or as
a function dstate within the call file
 3. Run call_dstate.m
 4. Analyze the results as a plot of state vs. t
function [t,y] = call_dstate()
tspan = [0 9]; % set time interval
y0 = 10; % set initial condition
% dstate evaluates r.h.s. of the ode
[t,y] = ode45(@dstate ,tspan ,y0);
plot(t,y)
disp ([t,y]) % displays t and y(t)
function dydt = dstate (t,y)
alpha=2; gamma=0.0001;
dydt = alpha*y-gamma *y^2;
end
end
Euler example
Matlab Code
 function [t,Y]=myeuler(f,interval,y0,M)
 % solves y'=f(x,y), one dimension
 % Input - f entered as @f (if M-file) or f (if anonymous funct)
 % - interval is a vector [a,b]
 % - y0 is the initial condition y(a)
 % - M is the number of steps
 % Output - t is the vector of abscissas and
 % Y is the solution vector
 h=(interval(2)-interval(1))/M; % step size
 Y=zeros(M+1,1); % solution will be column vector
 t=(interval(1):h:interval(2))'; % column vector independent variable
 Y(1)=y0; % initial value
 for j=1:M
 Y(j+1)=Y(j)+h*f(t(j),Y(j)); % Euler's équation
 end

Vous aimerez peut-être aussi