You are on page 1of 8

# ASSIGNMENT-2

## 1. WRITE A CODE FOR THE FOLLWING ALGORITHMS:

(A) BISECTION METHOD
Main file:
clc
clear
tol=0.00001;
x1=0;
x0=1;
k1=51;
while abs(k1)>tol
k=(x0+x1)/2;
k1=func(k);
if(k1<0)

x0=k;
end
if(k1>0)
x1=k;
end
end
k
function file:
function y0=func(x)
y0=x^2-3*x+1;
end
output:
k= 0.3820

## (B) NEWTON RAPHSON METHOD

Main fail:
clc
clear
tol=0.0001;
x0=0.2;
y=func(x0);
while abs(y)>tol
y=func(x0);
u=func1(x0);
x1=x0-(y/u);
x0=x1;
end
x0
function file 1:
function y0=func(x)
%y0=x^2+2*x+1;
y0=x*exp(x)-2;
end
function file 2:
function y1=func1(x)
y1=exp(x)+(x*exp(x));
end
output:
x0= 0.8526

## (C) SECANT METHOD

Main file:
clc
clear
tol=0.00001;
x0=-3;
x1=3;
k1=50;
while abs(k1)>tol
k=x1-((x1-x0)*(x1^3-2)/(x1^3-x0^3));
k1=func(k);
if(k1<0)
x0=k;
end
if(k1>0)
x1=k;
end
end
k
function file:
function y = func(x)
y=x^3-2;
end
output:
k= 1.2599

## 2. WRITE A CODE TO CHECK WHETHER AN ENTERED NUMBER IS ODD

OR EVEN?
Main file:
clc

clear
a=input('the number')
b=0
for i=0:a/2
if( a == i*2)
b=1;
end
end
if(b == 0)
disp('odd')
else
disp('even')
end
output:
the number 5
odd
the number 24
even
3. ACCEPT ANY NUMBER FROM THE USER. DISPLAY WHETHER THE
NUMBER IS DIVISIBLE BY 100 OR NOT.
Main file:
clc
clear
x=input('enter a number')
if(rem(x,100)==0)
disp('number is divisible by 100')
else
disp('not divisble by 100')
end
output:
enter a number 250
not divisible by 100

## 4. WRITE A CODE THAT DISPLAY ALL PRIME NUMBERS BETWEEN 50 AND

150?
Main file:
clc
clear
p=50;
k=0;

while p<150
r=p/2;
for i=2:r
if(rem(p,i) == 0)
k=k+1;
end
end
if(k == 0)
disp('is prime');
end
k=0;
p=p+1;
end
output:
53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 139 149
5. WRITE A PROGRAM TO ND THE DETERMINANT OF ANY NN MATRIX.
Main file:
clc
clear
a=[2 2 3;4 5 6;7 8 9];
b=det(a)
output:
-3

## 6. WRITE A PROGRAM TO SWAP THE VALES OF TWO NUMBERS. DO THIS

USING CALL BY METHOD OF FUNCTION.
Main file:
clc
clear
x=input('1st number');
y=input('2nd number');
[x1,y1]=func1(x,y);
x1
y1
function file:
function a[x1 y1]=func1(x,x2)
=x2;

end
output:
1st number 3
2nd number 5
x1=5, y1=3

## 7. USE FUNCTION FILE TO WRITE THE FOLLOWING CODES?

(a) Compute the maximum entry of a 65 size array.
(b) Compute the total sum of all the entries of a 65 size array.
Main file:
clc
clear
a=[1 2 3 4 5;4 5 6 7 8;7 8 9 10 11;
2 13 14 15 6;17 18 19 20 21;22 23 24 25 26];
max=0;
sum=0;
for i=1:6
for j=1:5
if(a(i,j)>max)
max=a(i,j);
end
sum=sum+a(i,j);
end
end
max
sum
output:
Max=26, sum=355

(c) Search for value key (i.e. ,user input) of a 65 size array. The code should return
true if found false otherwise.
(d) Search for value key (i.e., user input) of a 65 size array. The code should return
the row cardinality of location the value was found at if found and return1 if not
found.
(e) Search for value key (i.e., user input) of a 65 size array. The function should
return the column cardinality of location the value was found at if found and
Main file:
clc
clear
a=[1 2 3;4 5 6;7 8 9]
b=-1;
c=-1;
d=input('enter');
for i=1:3
for j=1:3
if(a(i,j) == d)
b=i;
c=j;
disp('true');
else
disp('false');
end
end
end
b
c
output:
enter 6
true
2
3

(f) Write a code that computes that maximum of a specific row Rn (user specific row
number) in a 6*5 array.

(g) Write acode that computes that total sum of a specic column Cn (user specic
column number) in 65 size array.
Main file:
clc
clear
a=[1 2 3 4 5;4 5 6 7 8;7 8 9 10 11; 2 13 14 15 6;17 18 19 20 21;22 23 24 25 26];
ro=input('enter a row for max');
colum=input('enter a column for max');
maxr=0;
maxc=0;
for i=1:5
if(a(ro,i)>maxr)
maxr=a(ro,i);
end
end
for j=1:6
if(a(j,colum)>maxc)
maxc=a(j,colum);
end
end
maxr
maxc
Output:
Enter a row for max 2
Enter a column for max 3
Maxr=8, Maxc=24