Johdatus sovellusprojekteihin, harjoitus 2
Tuomo Smolander
1.
>> format bank
>> A=[3 12 1;12 0 2;0 2 3];
>> B=[2.36;5.26;2.77];
>> X=A\B
X =
0.29
0.05
0.89
2.
a)
x=[25 12.5 5 2.5];
hold on; plot([x,[1 2 5 10],'o')
xev=linspace(0,30);
y1=lagrange(x,xev,0);
plot(xev,y1);
y2=lagrange(x,xev,1);
plot(xev,2.*y2);
y3=lagrange(x,xev,2);
plot(xev,5.*y3);
y4=lagrange(x,xev,3);
plot(xev,10.*y4);
grid;
y=y1+2.*y2+5.*y3+10.*y4;
plot(xev,y,'--');
c)
x=[25 12.5 5 2.5];
y=[1 2 5 10];
hold on; plot(x,y,'o')
plot(xev,polyval(polyfit(x,y,3),xev));
plot(x,y,'o',xev,[pchip(x,y,xev); spline(x,y,xev)]);
3.
a)
x=linspace(-5,5);
xn=linspace(-1,1,12);
t=(x-xn(1)).*(x-xn(2)).*(x-xn(3)).*(x-xn(4)).*(x-xn(5)).*(x-xn(6)).*(x-xn(7)).*(x-xn(8))
.*(x-xn(9)).*(x-xn(10)).*(x-xn(11)).*(x-xn(12));
plot(x,t);
grid;
b)
>> T=[0 0 0 0 0 0 1
0 0 0 0 0 1 0
0 0 0 0 2 0 -1
0 0 0 4 0 -3 0
0 0 8 0 -8 0 1
0 16 0 -20 0 5 0
32 0 -48 0 18 0 -1]
T =
0 0 0 0 0 0 1
0 0 0 0 0 1 0
0 0 0 0 2 0 -1
0 0 0 4 0 -3 0
0 0 8 0 -8 0 1
0 16 0 -20 0 5 0
32 0 -48 0 18 0 -1
>> TP=ones(7);
>> for n=1:7, TP(n,:)=T(n,:)./(2.^(n-1)),end;
>> TP
TP =
0 0 0 0 0 0 1.0000
0 0 0 0 0 0.5000 0
0 0 0 0 0.5000 0 -0.2500
0 0 0 0.5000 0 -0.3750 0
0 0 0.5000 0 -0.5000 0 0.0625
0 0.5000 0 -0.6250 0 0.1562 0
0.5000 0 -0.7500 0 0.2812 0 -0.0156
>> x=linspace(-5,5);
>> hold on;
>> for n=1:7, y=polyval(TP(n,:),x);plot(x,y),end;
c)
Osoitetaan (theta muutettu b:ksi):
cos(n+1)b = 2cos(b)cos(nb) - cos(n-1)b
cos(nb+b) = cos(nb)cos(b) - sin(nb)sin(b)
cos(nb-b) = cos(nb)cos(b) + sin(nb)sin(b)
<=>
cos(nb)cos(b) - sin(nb)sin(b) = 2cos(b)cos(nb) - cos(nb)cos(b) - sin(nb)sin(b)
0 = 0
>> hold on;
>> for n=1:7, plot(x,cos(n*acos(x))./(2.^(n-1))),end;
Warning: Imaginary parts of complex X and/or Y arguments ignored.
>> grid;
d)
Tn(x) = cos(n*arccos(x)) = 0
<=> n*arccos(x) = -(pii/2) + k*pii
<=> x = cos(-pii/(2n) + k*pii/n) = cos((2k-1)*pii/(2n)) m.o.t.
k=1:12;
xn=cos((2.*k-1).*pi./(2.*12));
x=linspace(-5,5);
t=(x-xn(1)).*(x-xn(2)).*(x-xn(3)).*(x-xn(4)).*(x-xn(5)).*(x-xn(6)).*(x-xn(7)).*(x-xn(8))
.*(x-xn(9)).*(x-xn(10)).*(x-xn(11)).*(x-xn(12));
plot(x,t);
grid;
4.
Koska suurin matriisiin tuleva luku 2000^10 hyvin suuri, ja matlab esittää kaikki luvut
tämän tarkkuudella, menee niistä suurin osa nollaan:
>> t = 1900:10:2000; V = vander(t)
V =
1.0e+33 *
Columns 1 through 8
0.6131 0.0003 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.6462 0.0003 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.6808 0.0004 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.7171 0.0004 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.7551 0.0004 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.7950 0.0004 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.8367 0.0004 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.8804 0.0004 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.9261 0.0005 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.9739 0.0005 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
1.0240 0.0005 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
>> cond(V)
ans =
2.7547e+48
>> mu = mean(t), sigma = std(t)
mu =
1950
sigma =
33.1662
>> s=(t-mu)./sigma
s =
Columns 1 through 8
-1.5076 -1.2060 -0.9045 -0.6030 -0.3015 0 0.3015 0.6030
Columns 9 through 11
0.9045 1.2060 1.5076
>> V=vander(s)
V =
Columns 1 through 8
60.6368 -40.2219 26.6802 -17.6977 11.7393 -7.7870 5.1653 -3.4263
6.5108 -5.3985 4.4762 -3.7115 3.0774 -2.5516 2.1157 -1.7542
0.3666 -0.4053 0.4481 -0.4954 0.5477 -0.6055 0.6694 -0.7401
0.0064 -0.0105 0.0175 -0.0290 0.0481 -0.0797 0.1322 -0.2193
0.0000 -0.0000 0.0001 -0.0002 0.0008 -0.0025 0.0083 -0.0274
0 0 0 0 0 0 0 0
0.0000 0.0000 0.0001 0.0002 0.0008 0.0025 0.0083 0.0274
0.0064 0.0105 0.0175 0.0290 0.0481 0.0797 0.1322 0.2193
0.3666 0.4053 0.4481 0.4954 0.5477 0.6055 0.6694 0.7401
6.5108 5.3985 4.4762 3.7115 3.0774 2.5516 2.1157 1.7542
60.6368 40.2219 26.6802 17.6977 11.7393 7.7870 5.1653 3.4263
Columns 9 through 11
2.2727 -1.5076 1.0000
1.4545 -1.2060 1.0000
0.8182 -0.9045 1.0000
0.3636 -0.6030 1.0000
0.0909 -0.3015 1.0000
0 0 1.0000
0.0909 0.3015 1.0000
0.3636 0.6030 1.0000
0.8182 0.9045 1.0000
1.4545 1.2060 1.0000
2.2727 1.5076 1.0000
>> cond(V)
ans =
1.9564e+04
5.
>> j=ones(1,12)*-1;
>> i=ones(1,15)*-1; i(4)=0; i(8)=0; i(12)=0;
>> h=eye(16)*4+diag(j,4)+diag(j,-4)+diag(i,1)+diag(i,-1);
>> b=[130 20 10 80 60 0 0 60 40 0 0 40 20 0 0 20];
>> T=h\b'
T =
52.0212
33.5727
28.1318
36.9939
44.5121
34.1379
31.9606
39.8439
31.8894
26.5061
25.7288
30.4212
16.5394
14.2682
14.0273
16.1121
>> TMAT=[0 50 20 10 0 0; 80 T(1) T(2) T(3) T(4) 80; 60 T(5) T(6) T(7) T(8) 60; 40 T(9) T(10) T(11)
T(12) 40; 20 T(13) T(14) T(15) T(16) 20; 0 0 0 0 0 0]
TMAT =
0 50.0000 20.0000 10.0000 0 0
80.0000 52.0212 33.5727 28.1318 36.9939 80.0000
60.0000 44.5121 34.1379 31.9606 39.8439 60.0000
40.0000 31.8894 26.5061 25.7288 30.4212 40.0000
20.0000 16.5394 14.2682 14.0273 16.1121 20.0000
0 0 0 0 0 0
>> surf(TMAT)