{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

matlab10

matlab10 - These 2 commands set up the euler’s method...

This preview shows pages 1–3. Sign up to view the full content.

1. >> t(1)=0, y(1)=1, h=.4 >> for n=1:5, y(n+1)=y(n)+h*(y(n)+2*t(n)), t(n+1)=t(n)+h, end These 2 commands set up the euler’s method calculation. >> a=0:0.04:2; >> b=3*exp(a)-2*a-2; These 2 commands set up the actual equation we are estimating. >> tc(1)=0; yc(1)=1; h=.04; >> for n=1:50; yc(n+1)=yc(n)+h*(yc(n)+2*tc(n)); tc(n+1)=tc(n)+h; end; These 2 commands set up a euler’s method calculation with a smaller step size >> plot(t,y,'r:p'); hold on; >> plot(tc,yc,'b:d'); plot(a,b,'g:s'); hold off; These two commands plot the 2 euler’s estimations and the actual function together. 0 0.5 1 1.5 2 2.5 0 2 4 6 8 10 12 14 16 18 2. >> t(1)=0, y(1)=1, h=.5 >> for n=1:12, y(n+1)=y(n)+h*(.5*y(n)+2*cos(t(n))), t(n+1)=t(n)+h, end, These 2 commands set up the euler’s method calculation with a .5 step size. >> plot(t,y,'r:p'); hold on; This command plots the euler’s estimation. >> t(1)=0, y(1)=1, h=.05

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
>> for n=1:120; y(n+1)=y(n)+h*(.5*y(n)+2*cos(t(n))); t(n+1)=t(n)+h; end;
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: These 2 commands set up the euler’s method calculation with a .05 step size. >> plot(t,y,'g:s'); This command plots the euler’s estimation on the same graph as the .5 step calculation. >> a=0:.05:6; >> b=.2*exp(a/2)+.8*cos(a)-(8/5)*sin(a); >> plot(a,b,'b:s'); These three commands define the actual function and plot it on the same graph as the approximations. 1 2 3 4 5 6-2-1 1 2 3 4 5 6 3. The two m-file functions are downloaded. Another m-file is created: Function yprime=horse(t,y) Yprime=(1/2)*y-2*cos(t); This file is saved as horse. >>[tr,yr]=rk4(‘horse’,[0,6],1,.05); This command defines the function using the m-file. >>plot(tr,yr,’b,s’) This command plots the rk4 approximation. We use the same commands as used previously along with rk4 commands to plot the rk4 and the euler approximation on the same graph as the actual equation. 1 2 3 4 5 6-2-1 1 2 3 4 5 6...
View Full Document

{[ snackBarMessage ]}

Page1 / 3

matlab10 - These 2 commands set up the euler’s method...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online