You are on page 1of 5

Homework #1 Solution

1. Calculate the value of the functions sine(x) and cosine(x) for the range 0 < x < 2p.

% Homework #1 Problem 1
%
clear all; clc
xx = [0:2*pi/100:2*pi];
yy = sin(xx); vv = cos(xx);
disp(' xx sine(xx) cosine(xx)')
for I=1:10:length(xx);
fprintf(' %6.3f %6.3f %6.3f \n', xx(I), yy(I), vv(I));
end
plot(xx,yy, xx,vv); xlabel('xx'); ylabel('sin(xx) & cos(xx)');
title('Plot of sine and cosine of xx')
disp(' ')

xx sine(xx) cosine(xx)
0.000 0.000 1.000
0.628 0.588 0.809
1.257 0.951 0.309
1.885 0.951 -0.309
2.513 0.588 -0.809
3.142 0.000 -1.000
3.770 -0.588 -0.809
4.398 -0.951 -0.309
5.027 -0.951 0.309
5.655 -0.588 0.809
6.283 -0.000 1.000

Plot of sine and cosine of xx


1

0.8

0.6

0.4

0.2
sin(xx) &cos(xx)

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7
xx
2.

% Present worth calculations


clear; clc
i=0.06;
n=1:25;
Annuity= 65000;
F=(1+i).^-n;
Present_W = Annuity *F;
disp(' ')
disp('Year Discount Factor Present Worth')
for k=1:length(n)
fprintf(' %2i %6.4f %8.0f \n', n(k), F(k), Present_W(k))
end
Total_Present_Worth=sum(Present_W);
fprintf('\n Total Present Worth %8.0f \n', Total_Present_Worth)

% Another way of calculating total present worth


% is the uniform-series present worth factor

PW= Annuity*((1+i)^25 - 1)/(i*(1+i)^25);

Year Discount Factor Present Worth


1 0.9434 61321
2 0.8900 57850
3 0.8396 54575
4 0.7921 51486
5 0.7473 48572
6 0.7050 45822
7 0.6651 43229
8 0.6274 40782
9 0.5919 38473
10 0.5584 36296
11 0.5268 34241
12 0.4970 32303
13 0.4688 30475
14 0.4423 28750
15 0.4173 27122
16 0.3936 25587
17 0.3714 24139
18 0.3503 22772
19 0.3305 21483
20 0.3118 20267
21 0.2942 19120
22 0.2775 18038
23 0.2618 17017
24 0.2470 16054
25 0.2330 15145

Total Present Worth 830918

From these results we conclude that Option A is better than Option B, because the total present worth of the
annuity (Option B) is lower than $1 million.
3. Use the command SOLVE:

>> clear; clc


>> x_symbolic=solve('a*x^3 + b*x^2 + c*x + d')
x_symbolic =

(((d/(2*a) + b^3/(27*a^3) - (b*c)/(6*a^2))^2 + (c/(3*a) - b^2/


(9*a^2))^3)^(1/2) - b^3/(27*a^3) - d/(2*a) + (b*c)/(6*a^2))^(1/3) - b/
(3*a) - (c/(3*a) - b^2/(9*a^2))/(((d/(2*a) + b^3/(27*a^3) - (b*c)/
(6*a^2))^2 + (c/(3*a) - b^2/(9*a^2))^3)^(1/2) - b^3/(27*a^3) - d/(2*a) +
(b*c)/(6*a^2))^(1/3)
(c/(3*a) - b^2/(9*a^2))/(2*(((d/(2*a) + b^3/(27*a^3) - (b*c)/(6*a^2))^2
+ (c/(3*a) - b^2/(9*a^2))^3)^(1/2) - b^3/(27*a^3) - d/(2*a) + (b*c)/
(6*a^2))^(1/3)) - b/(3*a) - (((d/(2*a) + b^3/(27*a^3) - (b*c)/(6*a^2))^2
+ (c/(3*a) - b^2/(9*a^2))^3)^(1/2) - b^3/(27*a^3) - d/(2*a) + (b*c)/
(6*a^2))^(1/3)/2 + (3^(1/2)*((c/(3*a) - b^2/(9*a^2))/(((d/(2*a) + b^3/
(27*a^3) - (b*c)/(6*a^2))^2 + (c/(3*a) - b^2/(9*a^2))^3)^(1/2) - b^3/
(27*a^3) - d/(2*a) + (b*c)/(6*a^2))^(1/3) + (((d/(2*a) + b^3/(27*a^3) -
(b*c)/(6*a^2))^2 + (c/(3*a) - b^2/(9*a^2))^3)^(1/2) - b^3/(27*a^3) - d/
(2*a) + (b*c)/(6*a^2))^(1/3))*i)/2
(c/(3*a) - b^2/(9*a^2))/(2*(((d/(2*a) + b^3/(27*a^3) - (b*c)/(6*a^2))^2
+ (c/(3*a) - b^2/(9*a^2))^3)^(1/2) - b^3/(27*a^3) - d/(2*a) + (b*c)/
(6*a^2))^(1/3)) - b/(3*a) - (((d/(2*a) + b^3/(27*a^3) - (b*c)/(6*a^2))^2
+ (c/(3*a) - b^2/(9*a^2))^3)^(1/2) - b^3/(27*a^3) - d/(2*a) + (b*c)/
(6*a^2))^(1/3)/2 - (3^(1/2)*((c/(3*a) - b^2/(9*a^2))/(((d/(2*a) + b^3/
(27*a^3) - (b*c)/(6*a^2))^2 + (c/(3*a) - b^2/(9*a^2))^3)^(1/2) - b^3/
(27*a^3) - d/(2*a) + (b*c)/(6*a^2))^(1/3) + (((d/(2*a) + b^3/(27*a^3) -
(b*c)/(6*a^2))^2 + (c/(3*a) - b^2/(9*a^2))^3)^(1/2) - b^3/(27*a^3) - d/
(2*a) + (b*c)/(6*a^2))^(1/3))*i)/2

>> a=1; b=-6; c=11; d=-6;


>> x=eval(x_symbolic)
x =
3.0000 + 0.0000i
1.0000 + 0.0000i
2.0000 - 0.0000i
>> eval('a*x.^3 + b*x.^2 + c.*x + d')
ans =
1.0e-015 *
0 + 0.1110i
0 + 0.1110i
0 + 0.1110i

All the solutions give a zero for the equation (note that 1.0e-015 is the multiplier for all
the roots).

Can also evaluate each root individually:


>> eval('a*x(1)^3+b*x(1)^2+c*x(1)+d')
ans =
0 +1.1102e-016i
>> eval('a*x(2)^3+b*x(2)^2+c*x(2)+d')
ans =
0 +1.1102e-016i
>> eval('a*x(3)^3+b*x(3)^2+c*x(3)+d')
ans =
0 +1.1102e-016i
>>
All the solutions give a zero for the equation.
4. Solve the two ODEs with initial conditions using DSOLVE

>> clear; clc


>> [y1,y2]=dsolve('Dy1 + 5*y2-y1 = exp(-t)','Dy2+y2-y1=0','y1(0)=1,
y2(0)=2')
y1 =
cos(2*t) - 4*sin(2*t)
y2 =
(9*cos(2*t))/5 + 1/(5*exp(t)) - (2*sin(2*t))/5

Verify:

>> diff(y1)+5*y2-y1
ans =
1/exp(t)

>> diff(y2)+y2-y1
ans =
0

>> t=[0:.01:10]; yy1=eval(y1); yy2=eval(y2);


>> plot(t,yy1, t, yy2); xlabel('t'); ylabel('y'); title('Solution of two
differential equations'); disp(' ')

Solution of two differential equations


5

0
y

-1

-2

-3

-4

-5
0 1 2 3 4 5 6 7 8 9 10
t

You might also like