Uploaded by Esraa Mohamed

اكواد التحليل العددي

advertisement
Program for Fixed point Method
function r =myfixed(x0,tol,n)
for i=1:n
x=g(x0);
if abs (x-x0) < tol
break
end
x0=x;
end
r=x;
function y=g(x)
y=x^2;
>> myfixed(1,0.00001,10000)
ans =
1
>> myfixed(0,0.00001,10000)
ans =
0
Program for Newton Method
function r =mynewt(x0,tol,n)
for i=1:n
x=x0-f(x0)/fd(x0);
if abs (x-x0) < tol
break
end
x0=x;
end
r=x;
function y=f(x)
y=x^2-4;
function t=fd(x)
t=2*x;
r =mynewt(1,0.00001,10000)
r=
2.0000
Program for bisection Method
function[p]=mybisection (a,b,tol,n)
f=inline ('sqrt (x)-cos (x)');
for i=1:n
p=(a+b)/2;
if abs (f (p))<tol
return
elseif f (a)*f (p)<0
b=p;
else
a=p;
end
end
mybisection (0,1,0.0001,100)
ans =
0.6417
‫برنامج بلغة الماتالب لحل التكامل األحادي بطريقة شبه المنحرف‬
function r =mytrip(a,b,n)
h=(b-a)/n;
sum =0;
for i=1:n-1
sum = sum +f(a+i*h);
end
I =h/2*(2* sum +f(a)+f(b));
disp('the integeration of the function =')
disp(I)
function y=f(x)
y=x;
>> mytrip(0,1,10)
the integeration of the function =
0.5000
‫برنامج بلغة الماتالب لحل التكامل األحادي بطريقة سمبسون‬
function r=mysimp(a,b,n)
h=(b-a)/n;
sum1 =0;
for i=1:2:n-1
sum1 = sum1 +f(a+i*h);
end
sum2 = 0;
for i=2:2:n-2
sum2 = sum2+f(a+i*h);
end
I =h/3*(4*sum1 +2* sum2 +f(a)+f(b));
disp('the integeration of the function =')
disp(I)
function y=f(x)
y=x;
>> mysimp(0,1,10)
the integeration of the function =
0.5000
Program for Euler Method
function y=myeuler(a,b,n,alpha)
f=inline('1-t*y');
h=(b-a)/n;
t(1)=a;
y(1)=alpha;
for i=2:n
t(i)=a+i*h;
y(i)=y(i-1)+h*f(t(i-1),y(i-1));
end
>> myeuler(0.0,5.0,25,1.0)
ans =
Columns 1 through 17
1.0000 1.2000 1.3040 1.3475 1.3319 1.2655
1.0365 0.9048 0.7791 0.6675 0.5738 0.4984 0.4392
0.3573 0.3286
1.1618
0.3933
Columns 18 through 25
0.3052
0.2087
0.2854
0.2685
0.2537
0.2406
0.2289
0.2183
Program for Range Kutta 4 Method
function y = myrk4(a,b,n,alpha)
f=inline('1-t*y');
t(1)=a;
y(1)=alpha;
h =(b-a)/n;
for i = 1 : n
k1 = h*f(t(i),y(i));
k2 = h*f(t(i) + h/2, y(i) +
k3 = h*f(t(i) + h/2, y(i) +
k4 = h*f(t(i) + h,
y(i) +
y(i+1) = y(i) + (k1 + 2*(k2
/ 6.0;
t(i+1)=a+i*h;
end
>>myrk4(0.0,5.0,25,1.0)
ans =
Columns 1 through 6
1.0000 1.1776 1.3024 1.3682 1.3755 1.3313
Columns 7 through 12
1.2473 1.1373 1.0147 0.8912 0.7753 0.6722
Columns 13 through 18
0.5842 0.5112 0.4519 0.4043 0.3661 0.3352
Columns 19 through 24
0.3098 0.2887 0.2708 0.2553 0.2418 0.2297
Columns 25 through 26
0.2190 0.2093
k1/2);
k2/2);
k3);
+ k3) + k4)
Download