MATLAB - Tutorial I

advertisement
MATLAB
TUTORIAL-I
1+1
ans =
2
>> x=sqrt(pi)
x=
1.77245385090552
>> xs=1:0.2:4
xs =
Columns 1 through 6
1.00000000000000 1.20000000000000 1.40000000000000 1.60000000000000
1.80000000000000 2.00000000000000
Columns 7 through 12
2.20000000000000 2.40000000000000 2.60000000000000 2.80000000000000
3.00000000000000 3.20000000000000
Columns 13 through 16
3.40000000000000 3.60000000000000 3.80000000000000 4.00000000000000
>> x=1:0.2:10;
>> y=sin(x);
>> plot(x,y);
>> plot(x,y,'-o')
>> %polinomlar
>> z=[1 -6 -72 -27]
z=
1 -6 -72 -27
>> % z=x^3-6x^2-72x-27
>> r=roots(z)
1
r=
12.12289378463240
-5.73450994222507
-0.38838384240732
>> % z=x^3-6x^2-72x-27
>> r=roots(z)
r=
12.12289378463240
-5.73450994222507
-0.38838384240732
>> %p=3x^2+2x+1
>> p=[3 2 1];
>> r2=roots(p)
r2 =
-0.33333333333333 + 0.47140452079103i
-0.33333333333333 - 0.47140452079103i
>> % polinomlarin değerlerini veren fonksiyon da vardır
>> polyval(p, [5 7 9])
ans =
86 162 262
SAYISAL INTEGRASYON
>> % sinx fonsiyonunun x=0 dan x=pi kadar olan integrali trapez ile
>> x=0:pi/100:pi;
>> y=sin(x);
>> z=trapz(x,y) %trapz(kime göre, kimin)
z=
1.99983550388744
2
>> % Bir onceki integralin simpson ile alinmasi
>> f1=inline('sinx')
f1 =
Inline function:
f1(sinx) = sinx
>> z=quad(f,0,pi) %quad(kimin, baslangic, son)
z=
9.41877659790813
PROGRAM YAZMA
Örnek 1: “ 10 kere ekranda merhaba dunya gorunmesini istiyorsak”
>> for n=1:10
disp('merhaba dunya');
end
merhaba dunya
merhaba dunya
merhaba dunya
merhaba dunya
merhaba dunya
merhaba dunya
merhaba dunya
merhaba dunya
merhaba dunya
merhaba dunya
Örnek 2: “ Matlabda 1 sayisini sürekli olarak 2 ile carptimizda elde edecegimiz en
büyük sayiyi bulmak istersek
>> format long
>> n=1;
>> for i=1:10000,
m=n; n=n*2;
if n == Inf, break; end,
end, m, i
m=
8.988465674311580e+307
i=
1024
3
İKİYE BOLME YONTEMİ
Ikiye bolme yontemi ie cosx=6x^2 -5 kokunu bulmak
>> TOL=5e-6;
>> f=inline('cos(x)-6*x.^2+5');
>> x=-1:0.01:1; plot(x, f(x), x, 0*x);
>> %% baslangic sartlarini secmek icin fonksiyon cizildi...
>>
>> a=-1; b=0;
>> bi=[a f(a) b f(b)];
>> while (b-a)/2 > TOL
c=(a+b)/2;
if f(c) == 0 break, end;
if f(a)*f(c)<0, b=c;
else a=c; end;
bi=[bi; a f(a) b f(b)];
end;
>> bi
bi =
-1.00000000000000
-1.00000000000000
-1.00000000000000
-1.00000000000000
-1.00000000000000
-0.96875000000000
…..
-0.96355438232422
-0.45969769413186
-0.45969769413186
-0.45969769413186
-0.45969769413186
-0.45969769413186
-0.06452917860669
0 6.00000000000000
-0.50000000000000 4.37758256189037
-0.75000000000000 2.35668886887382
-0.87500000000000 1.04724685816333
-0.93750000000000 0.31836757509248
-0.93750000000000 0.31836757509248
-0.00001763682127 -0.96354675292969 0.00007684430005
>> sol_c=(a+b)/2
sol_c =
-0.96355056762695
>>iteration_num=size(bi,1)
iteration_num =
18
4
NEWTON RAPHSON METODU
Fonksiyon f(x)=x^4-9.9x^3+35.09x^2-51.909x+26.3538
>> f=inline('x.^4-9.9*x.^3+35.09*x.^2-51.909*x+26.3538')
f=
Inline function:
f(x) = x.^4-9.9*x.^3+35.09*x.^2-51.909*x+26.3538
>> f1=inline('4*x.^3-9.9*3*x.^2+35.09*2*x-51.909')
f1 =
Inline function:
f1(x) = 4*x.^3-9.9*3*x.^2+35.09*2*x-51.909
>> x=1;r=1.1;e=r-x; xi=[];
>> for i=1:5, x=x-f(x)/f1(x);
xi=[xi; x (r-x)/e^2];
e=r-x;
end
>> xi
xi =
1.08544891640867
1.09962853794553
1.09999974935266
1.09999999999989
1.10000000000000
1.45510835913305
1.75438047444312
1.81649492632491
1.78486510821081
0
Farklı bir başlangıç değeri kullanıldığında:
>> x=2.3; r=2.2; e=r-x; xi=[];
>> for i=1:4, x=x-f(x)/f1(x);
xi=[xi; x (r-x)/e^2];
e=r-x;
end
>> xi
xi =
2.18679245283021
2.19984885857987
2.19999997924438
2.19999999999998
1.32075471697949
0.86644132481660
0.90859178197255
47.41944315779641
5
Download