Contents
Analytisk lösning till ordinär differentialekvation
t = linspace(0,2);
figure(1);clf
plot(t,exp(t),'linewidth',2);
Olika begynnelsevärden ger olika värden pä konstanten
hold on
plot(t,2*exp(t),'r');
plot(t,3*exp(t),'k');
l=legend('$u(t) = e^t$','$u(t) = 2e^t$','$u(t) = 3e^t$','location','NorthWest');
l.FontSize=17;
l.Interpreter='latex';
Eulers metod
clear all
f = @(t,u)u;
a = 0; b = 2; ua = 1;
N = 4; h = (b-a)/N;
t = a + (0:N)*h;
U(1) = ua;
for i = 1:N
U(i+1) = U(i) + h*f(t(i),U(i));
end
plot(t,U,'b*')
clear U
N = 40; h = (b-a)/N;
t = a + (0:N)*h;
U(1) = ua;
for i = 1:N
U(i+1) = U(i) + h*f(t(i),U(i));
end
plot(t,U,'-*c')
t=0:0.25:1.75; y=0:1:20;
[T,Y]=meshgrid(t,y);
dT=ones(size(T));
dY=Y;
q=quiver(T,Y,dT,dY,3,'ShowArrowHead', 'off');
axis([0,2,0,25])