Mat Lab 2
Mat Lab 2
Mat Lab 2
8. TRAPEZOIDAL RULE
11. LU FACTORIZATION
Aim:
Algorithm:
Example
Find the roots of x^3-x-1 using bisection method and plot the error
MATLAB Code:
f=@(x) x^3-x-
1; a=1;
b=2;
for
i=1:100
c=(a+b)/2
; if f(c)>0
b=c;
else
a=c;
end
end
a=1; b=2;
p=c; for
i=1:100
c=(a+b)/2;
er(i)=f(c)-
f(p); if f(c)>0
b=c;
else
a=c;
end
end
fprintf('Root of given equation is %f',c) plot(er);
title('Plot of error')
xlabel('Number of iterations')
ylabel('Error')
grid on;:
Result:
Aim:
clc;
x2(i)=x0-(x1-x0)/(f(x1)-f(x0))*f(x0);
if f(x2(i))>0
end
p=x2(i);
end
for i=1:10
error(i)=p-x2(i);
end
Answer=p
title('Plot of error');
xlabel('iterations');
ylabel('Error');
Result:
Aim:
MATLAB Program:
% Newton Raphson
Close all
clc
x=a;
for i=1:1:100
x1=x-
(f(x)/df(x));
x=x1;
end
sol=x;
x=a;
er(5)=0;
for i=1:1:5
x1=x-(f(x)/df(x));
x=x1;
er(i)=x1-sol;
end
plot(er)
xlabel('Number of
iterations') ylabel('Error')
Result:
AIM:
MATLAB Code:
Example:
-3 to 3
clc;
clear all;
close all;
n=b-a; %Number of
intervals h=(b-a)/n;
p=0;
for i=a:b
p=p+1; x(p)=i;
function end
l=length(x)
;x
y answer=(3*h/8)*((y(1)+y(l))+3*(y(2)+y(3)+y(5)+y(6))
+2*(y(4)))
Result:
Ex.no: Date:
AIM:
To find out the roots of the equation using Jacobbi iteration method in Matlab
MATLAB Code:
method: clc;
f1=0(x,y,z) (1/20)*(17-y+2*z);
f2=0(x,y,z) (1/20)*(-16-3*x+z);
f3=0(x,y,z) (1/20)*(25-2*x+3*z);
for i=1:5
f1(x,y,z);
f2(x,y,z);
f3(x,y,z);
a=f1 (x,y,z) ;
b=f2(x,y,z);
c=f3(x,y,z);
x=a; y=b;
z=c; end
z
Result:
Ex.no: Date:
Aim:
MATLAB Code:
end
Result:
Ex.no: Date:
AIM:
MATLAB Code:
-3 clc;
clear all;
close all;
n=b-a; %Number of
intervals h=(b-a)/n;
p=0;
for
i=a:b
p=p+1
x(p)=i
function end
l=length(x);
x
y
answer=(h/3)*((y(1)+y(l))+2*(y(3)+y(5))+4*(y(2)+y(4)+y(6)))
Result:
Ex.no: Date:
AIM:
MATLAB Code:
-3 clc;
clear all;
close all;
n=b-a; %Number of
intervals h=(b-a)/n;
p=0;
for i=a:b
p=p+1;
x(p)=i;
function end
l=length(x);
answer=(h/2)*((y(1)+y(l))+2*(sum(y)-y(1)-y(l)))
Result:
Ex.no: Date:
AIM:
[m,n]=size(a);
for j=1:m-1
for z=2:m
if a(j,j)==0
t=a(j,:);a(j,:)=a(z,:); a(z,:)=t;
end
end
for i=j+1:m
a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
end
end
x=zeros(1,m);
Result:
Ex.no: Date:
AIM:
To findout the roots of the given equation
MATLAB Code:
% Gauss-Jordan method
[m,n]=size(a);
for j=1:m-1
for z=2:m
if a(j,j)==0
t=a(1,:);a(1,:)=a(z,:);
a(z,:)=t;
End
End
for i=j+1:m
a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
End
end
for j=m:-1:2
for i=j-1:-1:1
a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
end
end
for s=1:m
a(s,:)=a(s,:)/a(s,s);
x(s)=a(s,n);
end
disp('Gauss-Jordan method:');
a
x'
Result:
AIM:
MATLABCode:
function [L,U] =
my_lu(A) n = size(A,
1);
I = eye(n); L = I;
U = A; for
k=1:n-1
L(k+1:n,k) = U(k+1:n,k)/U(k,k);
U(j,k:n) = U(j,k:n)-L(j,k)*U(k,k:n);
%rows end
end
= A; for k=1:n-1
L=eye(n);
for k=1:n
if (A(k,k) == 0) Error('Pivoting is
needed!'); end
L(k+1:n,k)=A(k+1:n,k)/A(k,k);
for j=k+1:n
A(j,:)=A(j,:)-L(j,k)*A(k,:);
end
end
Result:
Ex.no: Date:
AIM:
MATLAB Code :
function f = cstrrxns(C)
load cstrrxns.mat
\
Result:
AIM:
C-Program:
}
void main()
{
FILE *fp;
int i=0,count=-1;
float lower,upper,h,y1,xvalue[MAX],yvalue[MAX],result;
float function[MAX],search,final,temp;
fp=fopen("admbsh.dat","w");
clrscr();
printf("ADAM-BASHFORTH METHOD ");
fprintf(fp,"ADAM-BASHFORTH METHOD ");
printf("\n");
fprintf(fp,"\n");
printf("\nEnter the lower bound of x : ");
fprintf(fp,"\nEnter the lower bound of x : ");
scanf("%f",&lower);
fprintf(fp,"%f",lower);
printf("\nEnter the upper bound of x : ");
fprintf(fp,"\nEnter the upper bound of x : ");
scanf("%f",&upper);
fprintf(fp,"%f",upper);
printf("\nEnter the value of y(lower) : ");
fprintf(fp,"\nEnter the value of y(lower) : ");
scanf("%f",&y1);
fprintf(fp,"%f",y1);
printf("\nEnter the value of h : ");
fprintf(fp,"\nEnter the value of h : ");
scanf("%f",&h);
fprintf(fp,"%f",h);
printf("\nEnter the value of x for which you want to find y :");
fprintf(fp,"\nEnter the value of x for which you want to find y :");
scanf("%f",&search);
fprintf(fp,"%f",search);
xvalue[i]=lower;
yvalue[i]=y1;
for(i=0;xvalue[i]<=upper;i++)
{
xvalue[i+1]=xvalue[i]+h;
}
for(i=0;xvalue[i]<=upper;i++)
{
result=equation(xvalue[i],yvalue[i]);
yvalue[i+1]=yvalue[i]+(h*result);
printf("\n\n");
fprintf(fp,"\n\n");
printf("The table is ");
fprintf(fp,"The table is ");
printf("\n\n");
fprintf(fp,"\n\n");
printf(" i x y f(x,y) ");
fprintf(fp," i x y f(x,y) ");
printf("\n\n");
fprintf(fp,"\n\n");
for(i=0;xvalue[i]<=upper;i++)
{
function[i]=equation(xvalue[i],yvalue[i]);
printf(" %d. %.4f %.4f %.4f ",i,xvalue[i],yvalue[i],function[i]);
fprintf(fp," %d. %.4f %.4f %.4f ",i,xvalue[i],yvalue[i],function[i]);
count=count+1;
printf("\n");
fprintf(fp,"\n");
}
yvalue[search]=yvalue[count]+(h/24)*((-9*function[count-3])+(37*function[count-2])-
(59*function[count-1])+(55*function[count]));
final=equation(search,yvalue[search]);
yvalue[search]=yvalue[count]+(h/24)*((function[count-2])-(5*function[count-1])
+(19*function[count])+(9*final));
printf("\n\n");
fprintf(fp,"\n\n");
printf("Approximate value is : %.4f ",yvalue[search]);
fprintf(fp,"Approximate value is : %.4f ",yvalue[search]);
fclose(fp);
getch();
}
Result:
AIM:
C-Program:
Result: