You are on page 1of 6

Marc Hansen

Dr. Wang
MATH 238
10 April 2008
MatLab Project
Problem 1.
1. (a)

(b) When t=10,

t0=0;
tf=20;
y0=-pi^2;
[t,y]=ode45('F',[0,10],y0

(c)

y= -72.8882

plot (t,y,'*')

40

20

-20

-40

-60

-80

(d)

10

tspan=[0:.5:10]
y0=-pi^2;
[t,y]=ode45('F',tspan,y0);
y1= (t.^2.*cos(t))-(pi.^2.*cos(t));
plot(t,y,'*', t,y1,'-')

40

20

t=
0
0.5000
1.0000
1.5000
2.0000
2.5000
3.0000
3.5000
4.0000
4.5000
5.0000
5.5000
6.0000
6.5000
7.0000
7.5000
8.0000
8.5000
9.0000
9.5000
10.0000

y=
-9.8696
-8.4420
-4.7923
-0.5390
2.5937
3.1906
1.2202
-1.8888
-3.7698
-2.1114
3.9904
13.6898
24.0707
30.5798
28.7020
15.7100
-7.4010
-35.5880
-61.8344
-76.8975
-72.8882

y1 =
-9.8696
-8.4420
-4.7923
-0.5390
2.4426
2.8998
0.8609
-2.2291
-4.0071
-2.1881
4.2919
14.4430
25.0896
31.6223
29.5005
16.0771
-7.8760
-37.5537
-64.8091
-80.1531
-75.6258

-20

-40

-60

-80

-100

10

(e) The difference in part (a) and (d) comes from the fact
that t in part (d) is squared, therefore having more
weight than in part (a), but the graphs are identical until
the end of the interval. Another error comes from the
fact that (a) has tan function whereas (d) has a second
cosine function.

Problem 1
Graphs:
IVP Differential Equation
40

20

-20

-40

-60

-80

10

IVP Differential Equation w/ Exact Equation


40

20

-20

-40

-60

-80

-100

10

Problem 1
M-File: Saved as F.m
1.
2.

function yp=F(t,y);
yp=(2.*t*cos(t))-(y.*tan(t));

Errors:
Error 1:
Warning: Obsolete syntax. Use ode45(fun,tspan,y0,...) instead.
> In funfun\private\odearguments at 41
In ode45 at 173
??? Error using ==> feval
Undefined command/function 'F'.
Did not save function as F, and tried to run function as F
Error in ==> funfun\private\odearguments at 110
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ==> ode45 at 173
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, ...

Error 2:
Warning: Obsolete syntax. Use ode45(fun,tspan,y0,...) instead.
> In funfun\private\odearguments at 41

Tried to use an improper syntax for


computing the initial and final values of t.

Problem 2.
2. (a)

t=
0
0.2500
0.5000
0.7500
1.0000
1.2500
1.5000
1.7500
2.0000

tspan=[0:.25:2];
y0=-1; v0=0;
[t,Y]=ode45(@f1,tspan,[y0,v0]);
y=Y(:,1); v=Y(:,2);

(b)

plot (t,y,'b*-',t,v,'r*-')
Legend ('y(t)', 'v(t)=y''(t)')
0.8
0.6

y=
-1.0000
-0.9084
-0.6582
-0.3201
-0.0000
0.1774
0.0884
-0.3250
-1.1488

v=
0
0.0606
0.2194
0.4116
0.5403
0.4927
0.1591
-0.5031
-1.5648

0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

0.2

0.4

0.6

0.8

1.2

1.4

1.6

1.8

(c) Calculations are on separate sheet, but after using the method of undetermined coefficients
to get the exact solution, the exact solution is as follows:
( )
(d)

tspan=[0:.25:2];
y0=-1; v0=0;
[t,Y]=ode45(@f1,tspan,[y0,v0]);
y=Y(:,1); v=Y(:,2);
y1=((2./3).*cos(t))+ (sin(t)./3)+(exp(-3.*t)/2)-(3.*exp(-t))/2;
plot(t,y,'b*-',t,v,'r*-',t,y1,'g-')
0.8
legend('y(t)', 'v(t)=y''(t)','y(t) Exact')

y(t)
v(t)=y'(t)
y(t) Exact

0.6
0.4
0.2

(e) Although the exact solution (d) differs from


what was determined in part (a), the graphs
resemble the same general trend. In the given
interval, the exact graph seems to reach a
maximum by t=1 whereas the graph from (a)
seems to reach a maximum at t=2.

0
-0.2
-0.4
-0.6
-0.8
-1

0.2

0.4

0.6

0.8

1.2

1.4

1.6

1.8

Problem 2.
Graphs:
IVP Higher Order Differential Equation

0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

0.2

0.4

0.6

0.8

1.2

1.4

1.6

1.8

IVP Higher Order Differential Equation w/ Exact Solution

0.8
y(t)
v(t)=y'(t)
y(t) Exact

0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

0.2

0.4

0.6

0.8

1.2

1.4

1.6

1.8

Problem 2
M-File: Saved as F1.m
1.
2.
3.

function dYdt = f(t,Y)


y=Y(1); v=Y(2);
dYdt=[v; cos(t)-4.*v-3.*y];

Errors:
Error 1:
??? Error: File: C:\MATLAB7\work\F.m Line: 2 Column: 1
Function definitions are not permitted at the prompt or in scripts.
Error in ==> funfun\private\odearguments at 80
if (nargin(ode) == 2)

Forgot to change the variable in my


function from F, which was the first
problem, to F1, which was the M-file for
the second problem.

Error in ==> ode45 at 173


[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, ...

Error 2:
??? legend('y(t)', 'v(t)=y''(t)'
Error: Incomplete or misformed expression or statement.

I forgot to close the parenthesis at the


end of the command line.

You might also like