Vous êtes sur la page 1sur 22

# function [ maximum,minimum ] = Arr( array )

## %UNTITLED Summary of this function goes here

% Detailed explanation goes here
maximum=array(1);
minimum=array(1);
for i=1:length(array)
if array(i)>maximum
maximum=array(i);
end
if array(i)<minimum
minimum=array(i);
end

end

## function [ c ] = Bisection( f,a,b,n,tol )

%UNTITLED Summary of this function goes here
% Program of Bisection Method
fa=f(a);
fb=f(b);
if fa*fb>0
error('the roots have same sign ')
end
disp(' a b c fc error ')
for i=1:n
c=(a+b)/2;
fc=f(c);
diff=abs(b-a);
disp([ a b c fc diff])
if abs(b-a)<tol
disp([p])
break
end
if fa*fb>0
a=c;
fa=fc;
else
b=c;
fb=fc;
end
end
disp('The approximate root is :')

## function [c ] = Bisectionfinal( f,a,b,n,tol )

% UNTITLED5 Summary of this function goes here
% Detailed explanation goes here
% Find a real root of the equation x^3-2x-5=0 on the interval [2, 3]
% Input: [c ] = Bisectionfinal( @(x) x.^3-2*x-5,2,3,20,0.0001 )
fa=f(a);
fb=f(b);
if fa*fb>0
disp('The roots have same sign')
end
disp(' Bisection Method ')
disp(' i a b c fc diff ')
for i=1:n
c=(a+b)/2;
fc=f(c);
diff=abs(b-a);
disp([i a b c fc diff])
if abs(b-a)<tol
break
end
if fa*fc>0
a=c;
fa=fc;
else
b=c;
fb=fc;
end
end
disp(' The approximate root is : ')

clear all
close all
c=input('Enter a temperature in Celsius : ')
f=(9/5).*c+32;
disp('Temperature in Fahrenheit = ')
disp([f])

clear all
close all
theta=linspace(0,2*pi,100);
for r=0:10
x=r*cos(theta);
y=r*sin(theta);
plot(x,y,'r');
hold on
end
axis('equal');
xlabel('rcos(theta)')
ylabel('rsin(theta)')
gtext('Circles');

## function [capital,interest] = compound(x0,r,k,n)

% x0=capital,r= rate of interest,k=timescape,n=year
if r>1
disp('Check the interest rate')
end
capital=x0*(1+r/k)^(k*n);
interest=capital-x0;

## function [capital,interest] = compound(x0,r,k,n)

% x0=capital,r= rate of interest,k=timescape,n=year
if r>1
disp('Check the interest rate')
end
capital=x0*(1+r/k)^(k*n);
interest=capital-x0;
clear all
close all
n=input('Enter a number limit : ')
disp(' Divisor upto given limit ')
for i=1:n
if mod(n,i)==0
disp([i])
end
end

function[y] = Euler(f,e,x0,y0,h,b)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
n=(b-x0)/h;
disp(' i x y(Euler) y(Exact) Error')
for i=0:n
diff=abs(e(x0)-y0);
disp([ i x0 y0 e(x0) diff]);
y=y0+h*f(x0,y0);
x0=x0+h;
y0=y
end

## function [y] = Eulerfinal( f,e,x0,y0,h,b )

%UNTITLED Summary of this function goes here
% Detailed explanation goes here
n=(b-x0)/h;
disp(' i x y(euler) y(exact) Error')
for i=0:n
diff=abs(e(x0)-y0);
disp([ i x0 y0 e(x0) diff]),;
y=y0+h*f(x0,y0);
x0=x0+h;
y0=y;
end

## function [fact ] = factnum( n )

%UNTITLED Summary of this function goes here
% Detailed explanation goes here
fact=1.0;
for i=1:n;
fact=fact*i;
end
disp('factorial of the number %d is:',n)
disp([fact])

end

## function [fibo] = fibono( n )

%UNTITLED Summary of this function goes here
% Detailed explanation goes here
fibo=zeros(1,n);
fibo(1)=1;
fibo(2)=1;
k=3;
while k<=n
fibo(k)=fibo(k-2)+fibo(k-1);
k=k+1;
end
end
function [ x ] = Gauss( A,b )
% UNTITLED3 Summary of this function goes here
% Program of Gauss Elimination
N=length(b)
for column=1:(N-1)
for row =(column+1):N
d=A(row,column)/A(column,column);
A(row,:)=A(row,:)-d*A(column,:);
b(row)=b(row)-d*b(column);
end
end
for row=N:-1:1
x(row)=b(row);
for i=(row+1):N
x(row)=x(row)-A(row,i)*x(i);
end
x(row)=x(row)/A(row,row);
end
x=x';
return

function x = Gauss_1(A, b)
% Solve linear system Ax = b
% using Gaussian elimination without pivoting
% A is an n by n matrix
% b is an n by k matrix (k copies of n-vectors)
% x is an n by k matrix (k copies of solution vectors)
[n, n] = size(A); % Find size of matrix A
[n, k] = size(b); % Find size of matrix b
x = zeros(n,k); % Initialize x
for i = 1:n-1
m = -A(i+1:n,i)/A(i,i); % multipliers
A(i+1:n,:) = A(i+1:n,:) + m*A(i,:);
b(i+1:n,:) = b(i+1:n,:) + m*b(i,:);
end;

## % Use back substitution to find unknowns

x(n,:) = b(n,:)/A(n,n);
for i = n-1:-1:1
x(i,:) = (b(i,:) - A(i,i+1:n)*x(i+1:n,:))/A(i,i);
end

## function [ output ] = inverse( x )

%UNTITLED9 Summary of this function goes here
% Detailed explanation goes here
%enter avalue of x:
output=1/x;
disp('the required output is:')
end
function [ p] = Iteration( f,fd,a,n,tol );
% UNTITLED4 Summary of this function goes here
% Detailed explanation goes here
% Find a real root of the equation x^3=1-x^2 on the interval [0, 1] with
% an accuracy of 10^-4.
% Input: [ p] = Iteration( @(x) 1./sqrt(x+1),@(x) -1./(2*sqrt(x+1).^3),0.75,10,0.0004 )
if abs(fd(a))>=1.0
error('The Metod is diverge')
end
disp('Root found :')
disp(' n p ')
for i=1:n
p=f(a);
disp([ i p ])
if abs(p-a)<tol
break
end
a=p;
end
disp(' The approximate root is :')

## function [ y1 ] = Lagrange( x,y,xi );

%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
n=length(x)-1;
ni=length(xi);
L=ones(n+1,ni);
for k=0:n
for kk=0:(k-1)
L(kk+1,:)=L(kk+1,:).*(xi-x(k+1))/(x(kk+1)-x(k+1));
end
for kk=(k+1):n
L(kk+1,:)=L(kk+1,:).*(xi-x(k+1))/(x(kk+1)-x(k+1));
end
end
y1=y*L;
disp('The corresponding result is :')

function [ y1 ] = Lagrangefinal(x,y,xi )
% UNTITLED Summary of this function goes here
% Detailed explanation goes here
n=length(x)-1;
ni=length(xi);
L=ones(n+1,ni);
for k=0:n
for kk=0:k-1
L(kk+1,:)=L(kk+1,:).*(xi-x(k+1))/(x(kk+1)-x(k+1));
end
for kk=(k+1):n
L(kk+1,:)=L(kk+1,:).*(xi-x(k+1))/(x(kk+1)-x(k+1));
end
end
y1=y*L;
disp('the corresponding result is : ')

## function [ status] = leapyear( year )

%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
if mod(year,400)==0
status=true;
disp('the year is leapyear')
else if mod(year,4)==0&&mod(year,100)~=0
status=true;
disp('the year is leapyear')
else
status=false
disp('the year is not leapyear')
end
end

function [L,A]=LU_factor(A,n)
% LU factorization of an n by n matrix A
% using Gauss elimination without pivoting
% LU_factor.m
% A is factored as A = L*U
% Output:
% L is lower triangular with the main diagonal part = 1s.
% U is upper triangular and is stored in the original mtx A
% and must be zeroed out to get U
% K. Ming Leung, 01/26/03

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

clear all
close all
array=[3 -1 5 2 10 12 1];
maximum=array(1);
minimum=array(1);
for i=1:length(array)
if array(i)>maximum
maximum=array(i);
end
if array(i)<minimum
minimum=array(i);
end
end
disp('Maximum number is = ')
disp([maximum])
disp('Minimum number is = ')
disp([minimum])

## function [ p ] = Newton( f,fd,a,n,tol );

%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
if fd(a)==0.0
error('the method is failed')
end
disp('Root found')
disp(' n p')
for i=1:n
p=a-(f(a)/fd(a));
disp([ i p])
if abs(p-a)<tol
disp([p])
break
end
a=p;
end
disp('the approximate root is :')

## function [p] =Newtonfinal(f,fd,a,n,tol)

%UNTITLED Summary of this function goes here
% Detailed explanation goes here
if fd(a)==0.0
error('the method is failed')
end
disp('Root is found')
disp(' n p ')
for i=1:n
p=a-(f(a)/fd(a));
disp([ i p])
if abs(p-a)<tol
disp([p])
break
end
a=p;
end
disp('The approximate root is : ')

function [ a ] = numcheck( p )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
if p>0
disp('the number is positive')
else
disp('the number is negative')

end

## function [p] = Perfectf( n )

%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
sum=0;
for i=1:n
if mod(n,i)==0
sum=sum+i;
end
end
if sum==2*n
disp('the number is perfect')
else
disp('the number is not perfect')

end

function [ p ] = prime( n )
% m is initialization
%n is given number
for m=2:floor(sqrt(n))
if mod(n,m)==0
disp('The number is not prime');
p=n;
return
else if mod(n,m)~=0
disp('The number is prime');
p=n;
end
end
end

clear all
close all
a=input('Enter a number : ')
if isprime(a)
disp('The number is prime ')
else
disp('The number is not prime ')
end
ip=1:4;
f_z=0.0;
for ii=1:4
jj=find(ip~=ii);
prod=f(ip(ii));
for kk=jj
prod=prod*(z-x(ip(kk)))/(x(ip(ii))-x(ip(kk)));
end
f_z=f_z+prod;
end

## function [ b ] = Reverse( array )

%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
for i=1:length(array)
b(i)=array(length(array)-i+1);
end
disp('reverse array is :')
disp([b]);

clear all
close all
a=765;
b=str2num(fliplr(num2str(a)))

## function [ y ] = RK( f,e,x0,y0,h,b )

%UNTITLED2 Summary of this function goes here
% Program for RK4 method
n=(b-x0)/h;
disp(' i x y(RK) y(Exact) Error')
for i=0:n
diff=abs(e(x0)-y0);
disp([ i x0 y0 e(x0) diff])
k1=h*(f(x0,y0));
k2=h*(f((x0+h/2),(y0+k1/2)));
k3=h*(f((x0+h/2),(y0+k2/2)));
k4=h*(f((x0+h),(y0+k3)));
y=y0+(k1+2*k2+2*k3+k4)/6;
x0=x0+h;
y0=y;
end
function [y] = RKfinal( f,e,x0,y0,h,b )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
n=(b-x0)/h;
disp(' i x y(RK) y(exact) Error')
for i=0:n
diff=abs(e(x0)-y0);
disp([ i x0 y0 e(x0) diff])
k1=h*(f(x0,y0));
k2=h*(f((x0+h/2),(y0+k1/2)));
k3=h*(f((x0+h/2),(y0+k2/2)));
k4=h*(f((x0+h),(y0+k3)));
y=y0*(k1+2*k2+2*k3+k4)/6;
x0=x0+h;
y0=y;
end

## function [ root1,root2 ] = Root( f,a,b,c )

%UNTITLED Summary of this function goes here
% Detailed explanation goes here
d=b^2-4*a*c;
if d>=0
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
else
m=-b/(2*a);
n=sqrt(d)/(2*a);
root1=m+i*n;
root2=m-i*n;
end
end
function [ c ] = secant( f,a,b,n,tol )
%UNTITLED5 Summary of this function goes here
% Detailed explanation goes here
fa=f(a);
fb=f(b);
if fa*fb>0.0
error (' The Roots have same sign ')
end
disp(' a b c fc error ')
for i=1:n
c=(a*fb-b*fa)/(fb-fa);
fc=f(c);
diff=abs(b-a);
disp([ a b c fc dff])
if abs(fc)<tol
disp([p])
break
end
if fa*fc>0
a=c;
fa=fc;
else
b=c;
fb=fc;
end
end
disp('The approximate root is :')

## function [ c ] = Secantfinal( f,a,b,n,tol )

%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
fa=f(a);
fb=f(b);
if fa*fb>0
disp('The roots have same sign')
end
disp(' i a b c fc diff')
for i=1:n
c=(a*fb-b*fa)/(fb-fa);
fc=f(c);
diff=abs(b-a);
disp([i a b c fc diff])
if abs(f(c))<tol
disp([c])
break
end
if fa*fc>0
a=c;
fa=fc;
else
b=c;
fb=fc;
end
end
disp('the approximate root is :')

## function [ Area ] = SimOT( f,a,b,n )

% UNTITLED4 Summary of this function goes here
% Progarm of Trapizoidal Rule
h=(b-a)/n;
sum1=f(a)+f(b);
sum2=0.0;
sum3=0.0;
for i=1:n-1
if mod(i,2)~=0.0
sum2=sum2+f(a+i*h);
else
sum3=sum3+f(a+i*h);
end
end
Area=(h/3)*(sum1+4*sum2+2*sum3);
disp(' The required area of Simpson 1/3 rule is :')
function [ Area ] = SimTE( f,a,b,n )
% UNTITLED4 Summary of this function goes here
% Progarm of Simpson 3/8 Rule
h=(b-a)/n;
sum1=f(a)+f(b);
sum2=0.0;
sum3=0.0;
for i=1:n-1
if mod(i,3)~=0.0
sum2=sum2+f(a+i*h);
else
sum3=sum3+f(a+i*h);
end
end
Area=((3*h)/8)*(sum1+3*sum2+2*sum3);
disp(' The required area of Simpson 3/8 rule is :')

clear all
close all
theta=linspace(0,10*pi,100);
r=exp(-theta/10);
polar(theta,r)

clear all
close all
sum1=0.0;
n=input('Enter the number limit :')
for i=1:n
sum1=sum1+i;
end
disp('the summation of numbers : ')
disp([sum1])

## function [ sum ] = summation( sum1,n )

% sum1=0.0
for i=1:n
sum1=sum1+i;
end
disp('the summation of numbers ')
sum=sum1;

## function [ Area ] = trapezoidal( f,a,b,n )

% UNTITLED4 Summary of this function goes here
% Progarm of Trapizoidal Rule

h=(b-a)/n;
sum1=f(a)+f(b);
sum2=0.0;
for i=1:n-1
sum2=sum2+f(a+i*h);
end
Area=(h/2)*(sum1+2*sum2);
disp(' The required area is :')

## function [ Area ] = Trapizoidalfinal( f,a,b,n )

%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
h=(b-a)/n;
sum1=(f(a)+f(b))/2;
sum2=0.0;
for i=0:n-1
sum2=sum2+f(a+i*h);
end
Area=h*(sum1+sum2);
disp('the value of area is : ')