KKKA05 Tenta 2011-03-10 Carmen Arevalo
Contents
format short g
Uppgift 1
A=[ 13 39 2 57 28
-4 -12 0 -19 -9
3 0 -9 2 1
6 17 9 5 7
19 42 -17 107 44];
B=[ -53 57 -145
18 -18 49
-7 -11 -27
0 18 -4
-103 69 -286];
[L,U,P]=lu(A)
L =
1 0 0 0 0
0.68421 1 0 0 0
0.31579 0.3641 1 0 0
0.15789 -0.64615 0.26499 1 0
-0.21053 -0.30769 0.065431 -0.0018642 1
U =
19 42 -17 107 44
0 10.263 13.632 -16.211 -2.1053
0 0 9.4051 -22.887 -6.1282
0 0 0 -19.304 -5.6838
0 0 0 0 0.0057621
P =
0 0 0 0 1
1 0 0 0 0
0 0 0 1 0
0 0 1 0 0
0 1 0 0 0
Solve Ly=PB
Y=L\(P*B);
Solve Ux=y
X=U\Y
X =
1 -1 1
-1 1 -2
1 1 3
-1 1 -2
1 -1 1
Check solution: norm of residual should be very small
norm(A*X-B)
ans = 3.2517e-014
Uppgift 2
f=@(x)x.^3.*sin(x)-x.^2-10*x+7; xp=linspace(-8,8); yp=f(xp); figure(1) plot(xp,yp) grid
There are 4 roots near -6, -4, 0 and 6.
[x1,r1]=fzero(f,-6) [x2,r2]=fzero(f,-4) [x3,r3]=fzero(f,0) [x4,r4]=fzero(f,6)
x1 =
-6.151
r1 =
4.2633e-014
x2 =
-3.7546
r2 =
0
x3 =
0.67369
r3 =
0
x4 =
6.6441
r4 =
-2.1316e-014
Uppgift 3
T=0:20:180; r=[-1.31 0 1.32 2.60 3.95 5.29 6.59 7.90 9.24 10.55];
The independent variable must be r, because we want a function T(r)
figure(2) plot(r,T,'o') xlabel('r') ylabel('T')
Fitting a straight line, y=p(1)x+p(2)
p=polyfit(r,T,1) disp('The fitted line is y = p(1) x + p(2)') rp=linspace(r(1),r(end)); Tp=polyval(p,rp); figure(3) plot(r,T,'o',rp,Tp) xlabel('r') ylabel('T') legend('data','fit','Location','best')
p =
15.163 20.052
The fitted line is y = p(1) x + p(2)