Вы находитесь на странице: 1из 5

Name:

Christy Lam
CID: 00930708

MATLAB Controlled Assessment : 21 November 2014



Section A: Mastery


1)
x = -pi :0.001: pi;
y1 = cos(x);
y2 = 1-((x.^2)/2) + (x.^4/24);
plot(x, y1, 'r');
hold on
plot(x, y2, 'b');
hold off
title('plot showing y = cos(x) and y = 1-x^2/2+x^4/24')
xlabel('x')
ylabel('y')
legend('y=cos(x)', 'y=1-x^2/2+x^4/24')
plot showing y = cos(x) and y = 1-x 2 /2+x4 /24

y=cos(x)
y=1-x 2/2+x 4/24

0.8
0.6
0.4

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

-3

-2

-1


2)
%assigning variables
n = 2
Vt = 0.7
Is = 8.3e-10
R = 1e+9
V = 0.5
%fzero
fzero(@(I) n*Vt*log(I/Is + 1) + I*R - V, 1e-09)
ans =
1.990492737220619e-10

I =
1.990492737220619e-10
3)
% assign matrix and vector
% m = matrix
m = [2 1 2; 3 2 0; -3 -2 -2]
% v = vector
v = [-2; 7; -1]
% x = vector of unknows x1, x2 and x3
% m*x = v
x = m\v
x =
1.0000
2.0000
-3.0000
x1 = 1
x2 = 2
x3 = -3

Section B

1)
% initialization
xa = 0.0; xb = 1.0;
f = @(x) x-exp(-x);
ya = f(xa); yb = f(xb);
% iteration 1
nextvalue = xb - yb*(xb-xa)/(yb-ya);
xa = xb; ya = yb;
xb = nextvalue; yb = f(nextvalue);
% iteration 2
nextvalue = xb - yb*(xb-xa)/(yb-ya);
xa = xb; ya = yb;
xb = nextvalue; yb = f(nextvalue);
% final value
xb
% approximate solution to f(x) = 0
xb =
0.563838389161074
2)
% initialization
xa = 0.0; xb = 1.0;
f = @(x) x-exp(-x);
ya = f(xa); yb = f(xb);
% iteration using for loop and iterating 6 times
for n = 1:6

nextvalue = xb - yb*(xb-xa)/(yb-ya);
xa = xb; ya = yb;
xb = nextvalue; yb = f(nextvalue);
end
% final value
xb
xb =
0.567143290409784
yb =
-1.110223024625157e-16
% how close to zero is the final value of yb?
0-yb
ans =
1.110223024625157e-16
3)
function solution = secantmethod(f, xa, xb, nIterations)
%secant method for finding an approximate solution of f(x) = 0
%initialisation
ya = f(xa); yb = f(xb);
for iteration = 1:nIterations
nextvalue = xb - yb*(xb-xa)/(yb-ya);
xa = xb; ya = yb;
xb = nextvalue; yb = f(nextvalue);
end
%final value
solution = xb
end
%test
test = secantmethod(@(x) x-exp(-x), 0.0, 1.0, 6)
solution =
0.567143290409784
test =
0.567143290409784

4)
secantmethod(@(x) x-exp(-x), 0.0, 1.0, 10)

solution =
NaN
NaN = not a number, undefined
ya and yb get too close together, yb-ya is near 0
(xb-xa)/(yb-ya)becomes undefined
xb - yb*(xb-xa)/(yb-ya) also becomes undefined

5)
function xvec = secantmethod2(f, xa, xb, nIterations)
%secant method for finding an approximate solution of f(x) = 0 that
returns
%a vector of all x-iterates
%initialisation
ya = f(xa); yb = f(xb);
xvec = [xa, xb, zeros(1,nIterations)]
for iteration = 1:nIterations
nextvalue = xb - yb*(xb-xa)/(yb-ya);
xa = xb; ya = yb;
xb = nextvalue; yb = f(nextvalue);
xvec(iteration+2) = xb;
end
xvec(nIterations) = xb;
end
test = secantmethod2(@(x) x-exp(-x), 0.0, 1.0, 6)
test =
Columns 1 through 5
0
0.563838389161074

1.000000000000000
0.567170358419745

0.612699836780282

Columns 6 through 8
0.567143290409784
0.567143290409705
0.567143290409784






6)
plot(ans)
xlabel('number of iterations')
ylabel('approximate solution')
title('plot showing the approximate solutions against number of
iterations')

plot showing the approximate solutions against number of iterations

1
0.9
0.8

approximate solution

0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

number of iterations


7)
%assigning variables
n = 2
Vt = 0.7
Is = 8.3e-10
R = 1e+9
V = 0.5
%setting xa and xb
xa = 1e-09
xb = 1
secantmethod2(@(I) n*Vt*log(I/Is +1) + I*R -V, 1e-09, 1, 10)
ans =
Columns 1 through 5
0.000000001000000
1.000000000000000
0.000000002339352
0.000000000696188

-0.000000000606904

Columns 6 through 10
0.000000000049742
0.000000000217896
0.000000000199045
0.000000000199049

0.000000000199879

Columns 11 through 12
0.000000000199049

0.000000000199049

answer from section A, q2:


I =
1.990492737220619e-10
Answer = 8 iterations required for solution to match answer from
section A q2 to six significant figures.

Вам также может понравиться