Clc % Bisection Method
clear all
format long
syms f(x)
f(x)= input("enter the function:") %x*sin(x)+3*cos(x)-x;
a=1;
b=1;
er=1;
while f(a)*f(b)>0
a=rand*10-5;
b=rand*10-5;
end
for i=1:100
p(i)=(a+b)/2
if f(a)*f(p(i))<0
b=p(i);
else
a=p(i);
end
er=abs(f(p(i)));
if er<10^(-3)
break
end
end
p
clc %yarılama Metodu -Bisection Method
clear all
format long
syms f(x)
f(x)=input("type the function/fonksiyonu giriniz: ");
%log(x^2+1)-5*cos(x)-3
a=rand*10-5;
%generates a random number between -5 and 5
b=rand*10-5;
% -5 ile 5 arasında rasgele bir sayı üretir.
while
f(a)*f(b)>0
a=rand*100-50;
b=rand*100-50;
end
err=1; %hatanın başlangıç değeri/initial value of the error
while
err>10^(-10)
p=(a+b)/2;
if f(a)*f(p)<0
b=p;
else
a=p;
end
err=abs(f(p));
end
p
clc %Newton Raphson Method
clear all
format long
syms f(x)
f(x)=input("type the function/fonksiyonu giriniz: ");
%log(x^2+1)-5*cos(x)-3
p0=rand*10-5;
%generates a random number between -5 and 5
df=diff(f);
% derivative of the f/ f'nin türevi
for i=1:100000
p=p0-f(p0)/df(p0);
mh=abs(p-p0);
% absolute error/ mutlak hata
p0=double(p);
if mh<10^(-7)
break
end
end
p0
clc %False Position Method
clear all
syms f(x)
f(x)=log(x^2+1)-5*cos(x)-3;
a=rand*10-5;
%attain a random value between -5 and 5
b=rand*10-5;
while
f(a)*f(b)>0
a=rand*10-5;
b=rand*10-5;
end
for i=1:1000000
p=double(b-f(b)*(b-a)/(f(b)-f(a)));
if f(a)*f(p)<0
b=p;
else
a=p;
end
if abs(f(p))<10^(-7)
break
end
end
p
clc % Secants Method
clear all
syms f(x)
f(x)=input("Enter the function: ");%log(x^2+1)-5*cos(x)-3;
p0= rand*10-5;
p1= rand*10-5;
for i=1:10
p=double(p1-f(p1)*(p1-p0)/ (f(p1)-f(p0)));
error=abs(p-p1)/abs(p1);
%Relative Error
p0=p1;
p1=p;
if error<10^(-7)
break
end
end
p
clc %Gauss elimination
clear all
A=[2 1 -1 2; 4 5 -3 6; -2 5 -2 6; 4 11 -4 8];
b=[5; 9; 4 ; 2];
n=length(A);
for i=1:n
A(i,n+1)=b(i);
end
for i=1:n
for k=i+1:n
m=A(k,i)/A(i,i);
for j=i:n+1
A(k,j)=A(k,j)-m*A(i,j);
end
end
end
for i=n:-1:1
s=0;
for j=i+1:n
s=s+A(i,j)*x(j);
end
x(i)=(A(i,n+1)-s)/A(i,i);
end
x
clc %Gauss elimination with partial pivoting
clear all
A=[2 1 -1 2; 4 5 -3 6; -2 5 -2 6; 4 11 -4 8];
b=[5; 9; 4 ; 2];
n=length(A);
for i=1:n
A(i,n+1)=b(i);
end
for i=1:n
richard=A(i,:);
[maks,indis]=max(abs(A(i:n,i)));
indis=indis+i-1;
if indis~=i
A(i,:)=A(indis,:);
A(indis,:)=richard;
end
for k=i+1:n
m=A(k,i)/A(i,i);
for j=i:n+1
A(k,j)=A(k,j)-m*A(i,j);
end
end
end
for i=n:-1:1
s=0;
for j=i+1:n
s=s+A(i,j)*x(j);
end
x(i)=(A(i,n+1)-s)/A(i,i);
end
x
clc %LU Decomposition Doolittle Method
clear all
A=[3 -7 -2 2; -3 5 1 0 ; 6 -4 0 -5; -9 5 -5 12];
b= [-9; 5 ; 7; 11 ];
n=length(A);
for i=1:n
L(i,i)=1;
%%%%%rows of U
for j=i:n
sumu=0;
for k=1:i-1
sumu=sumu+L(i,k)*U(k,j);
end
U(i,j)=A(i,j)-sumu;
end
%columns of L
for j=i+1:n
suml=0;
for k=1:i-1
suml=suml+L(j,k)*U(k,i);
end
if U(i,i)==0
'LU decomposition can not be applied'
break
end
L(j,i)=(A(j,i)-suml)/U(i,i);
end
end
%forward substitution to find y
for i=1:n
sumy=0;
for j=1:i-1
sumy=sumy+L(i,j)*y(j);
end
y(i)=b(i)-sumy;
end
%backward substitution to find x
for i=n:-1:1
sumx=0;
for k=i+1:n
sumx=sumx+U(i,k)*x(k);
end
x(i)=(y(i)-sumx)/U(i,i);
end
x
clc %Cholesky Decomposition
clear all
A=[16 4 16 -4
4 5 6 -9
16 6 33 -28
-4 -9 -28 58];
n=length(A);
if A==(A')
else
'the coefficient matrix is not symmetric'
end
for
j=1:n
d(j)=det(A(1:j,1:j));
if d(j)==0
'the coefficient matrix is not positive definite'
end
t=0;
for
k=1:j-1
t=t+L(j,k)^2;
end
L(j,j)=sqrt(A(j,j)-t);
for
i=j+1:n
s=0;
for
k=1:j-1
s=s+L(i,k)*L(j,k);
end
L(i,j)=(A(i,j)-s)/L(j,j);
end
end
L