SlideShare ist ein Scribd-Unternehmen logo
1 von 11
EC 313: Numerical Methods                            Mayank Awasthi
MATLAB Assignment – 1                               M. Tech, IIT Roorkee
                                                    (Communication Systems)

Ques1:

MATLAB routine which finds the coefficients of Lagrange polynomial is below:
%Implementing Lagrange Interpolating Formula
function l = lagranp(x,y)
N = length(x)-1; %the degree of polynomial
l = 0;
for m = 1:N + 1
P = 1;
for k = 1:N + 1
%Multiplication of two polynomials corresponds to convolution of their
coefficients.
if k ~= m

%”linconv” function created by own.
P = linconv(P,[1 -x(k)])/(x(m)-x(k));
end
end
l = l + y(m)*P; %Lagrange polynomial coefficients
end

MATLAB routine for Linear Convolution:
%Linear convolution of sequence using formula
function s=linconv(x,h)
N=length(x);
M=length(h);
for n=1:N+M-1
  s(n)=0;
for k=1:M
  a=n-k;
if a<0
   y(k)=0;
elseif k>N
    y(k)=0;
elseif a<M
   y(k)=x(k)*h(a+1);
else
   y(k)=0;
end
   s(n)=s(n)+y(k);
end
end
MATLAB routine which finds the coefficients of Newton polynomial is below:

%Implementing the Newton divided difference formula
function n = newtonp(x,y)
N = length(x)-1;
f = zeros(N+1,1);
f(1:N+1,1) = y;
for k = 2:N + 1
for m = 1: N + 2 - k %Divided Difference
f(m,k) = (f(m + 1,k - 1) - f(m,k - 1))/(x(m + k - 1)- x(m));
end
end
a = f(1,:);
n = a(N+1);
for k =N:-1:1
%Nested multiplication form for better computation efficiency
%a_k=f(x0,x1,


.xk)
n = [n a(k)] - [0 n*x(k)]; %f(x)*(a_k*(x - x(k - 1))+a_k – 1)
end

>> x=[1 3 4];                 Calculating the polynomial using both routines for
>> y=[1 27 64];               given data.

>> coefflag = lagranp(x,y)
coefflag =
8.0000 -19.0000 12.0000           So required polynomial we get 8x2 – 19x + 12

>> coeffnew = newtonp(x,y)
coeffnew =
8 -19 12
Ques2:

a)- For Straight Line: deg(Polynomial)=1
    So using Newton or Lagrange routine we can get the coeff. Of polynomial as
below:

>> x= [1970 1990] ;
>> y= [3707475887 5281653820] ;
>> nc=newtonp(x,y)
nc =
1.0e+011 *
0.0008 -1.5135

>> v0=polyval(nc,1980)

v0 =                                  % 1980 Population using straight line
4.4946e+009

b)- For parabola: deg(Polynomial)=2

>> x= [1960 1970 1990];
>> y= [3039585530 3707475887 5281653820];
>> nc= newtonp(x,y)
nc =
1.0e+012 *
 0.0000 -0.0015 1.4063

>> v1=polyval(nc,1980)                % 1980 Population parabola

v1 =
4.4548e+009

c)- For cubic curve deg(Polynomial)

>> x= [1960 1970 1990 2000];
>> y= [3039585530 3707475887 5281653820 6079603571];
>> nc=newtonp(x,y)
nc =
1.0e+013 *
 -0.0000 0.0000 -0.0107 7.0777

>> v3=polyval(nc,1980)                 % 1980 Population using cubic curve

v3 =
4.4729e+009
Ques4:

MATLAB routine which computes the divide difference coefficient is below:

%matalb script for computing dividedifference coefff.
function ddc = dividediff(x,y)
N = length(x)-1;
f = zeros(N+1,1);
f(1:N+1,1) = y;
for k = 2:N + 1
for m = 1: N + 2 - k %Divided Difference
f(m,k) = (f(m + 1,k - 1) - f(m,k - 1))/(x(m + k - 1)- x(m));
end
ddc=f(1,:);
end
end

Calculating the divide difference coefficients.
>> x=[1 2 4 5 7 9];
>> y=[2 3 8 6 14 6];
>> divide_diff_coeff = dividediff(x,y)

divide_diff_coeff =

  2.0000 1.0000 0.5000 -0.5000           0.2000 -0.0518

Now Calculating the polynomial using Newton method routine used in Ques1.
>> x=[1 2 4 5 7 9];
>> y=[2 3 8 6 14 6];
>> nc= newtonp(x,y)

nc =

 -0.0518 1.1839 -9.7875 35.6018 -53.4464 28.5000

Required Polynomial is
-0.0518x5 + 1.1839x4 -9.7875x3 + 35.6018x2-53.4464x +28.5000

v0=p(1.5), v1=p(3.7), v2=p(6.6), v3=p(9.5)

>> v0=polyval(nc,1.5)                             >> v2=polyval(nc,6.6)
v0 =                                              v2 =
1.0020                                            10.6441

>> v1=polyval(nc,3.7)                             >> v3=polyval(nc,9.5)
v1 =                                              v3 =
8.3474                               -21.6454

Plotting Graph
>> x=[1 2 4 5 7 9];
>> y=[2 3 8 6 14 6];
>> nc= newtonp(x,y)

nc =

 -0.0518 1.1839 -9.7875 35.6018 -53.4464 28.5000

>> m=[0:10];
>> n=polyval(nc,m)

n=
28.5000 2.0000 3.0000 7.6286 8.0000 6.0000      7.0714 14.0000 20.7000
6.0000 -72.5714

>> plot(m,n)
Ques5 :
Bisection Method :
%Bisection method to solve Non-linear equations.
function [x] = bisct(f,a,b,z,MaxIter)
fa = feval(f,a);
fb = feval(f,b);
if fa*fb > 0
error('We must have f(a)f(b)<0!');
end
for k = 1:MaxIter
xx(k) = (a + b)/2;
fx =feval(f,xx(k));
err = (b-a)/2;
if abs(err)<z
break;
elseif fx*fa > 0
a = xx(k); fa = fx;
else b = xx(k);
end
x = xx(k);
end
fprintf('The Interval in which root lie is %d   %dn',a,b)
fprintf('Iteration Required is %dn',k)
fprintf('The approx. error is %dn',err)
end

Regula Falsi Method :
%Implementing Regula Falsi algorithm.
function [] = falsp(f,a,b,z,MaxIter)
fa =feval(f,a);
fb=feval(f,b);
if fa*fb > 0
error('We must have f(a)f(b)<0!');
end
for k = 1: MaxIter
xx(k) = (a*fb-b*fa)/(fb-fa);
fx = feval(f,xx(k));
err = max(abs(xx(k) - a),abs(b - xx(k)));
if err<z
    break;
elseif fx*fa > 0
    a = xx(k);
    fa = fx;
else b = xx(k);
     fb = fx;
end
end
x = xx(k);
fprintf('The Interval in which root lie is %d   %dn',a,b)
fprintf('Iteration Required is %dn',k)
fprintf('The approx. error is %dn',err)
end
Modified Regula Falsi Method :
%Implementing Modified Regula Falsi algorithm
function [] = mrfalsp(f,a,b,z,MaxIter)
fa =feval(f,a);
fb=feval(f,b);
F=fa;
G=fb;

if fa*fb > 0
error('We must have f(a)f(b)<0!');
end
xx(1)=a/2;
for k = 1: MaxIter
xx(k+1) = (a*G-b*F)/(G-F);
fx = feval(f,xx(k+1));
err = max(abs(xx(k) - a),abs(b - xx(k)));
if err<z
    break;
end
if fx*fa > 0
    a = xx(k+1);
    fa = fx;
elseif feval(f,xx(k))*feval(f,xx(k+1))>0
    F=F/2;
    end
if fx*fa < 0
    b = xx(k+1);
     fb = fx;
    elseif feval(f,xx(k))*feval(f,xx(k+1))>0
    G=G/2;
    end
end
fprintf('The Interval in which root lie is %d %dn',a,b)
fprintf('Iteration Required is %dn',k)
fprintf('The approx. error is %dn',err)
end
Newton Raphson Meyhod :
%Implementing Newton Raphson Method
function [] = newton(f,df,x0,z,MaxIter)
xx(1) = x0;
fx = feval(f,x0);
for k = 1: MaxIter
if ~isnumeric(df)
    dfdx = feval(df,xx(k));end
dx = -fx/dfdx;
xx(k+1) = xx(k)+dx;
fx = feval(f,xx(k + 1));
a= xx;
if abs(dx) < z
break; end
end
l=length(a);
fprintf('The approx. Root is %dnn',x);
fprintf('Iteration Required is %dn',k)
end

Now calculating the roots of given polynomials
a)- exp(-x)-sin(x)                  assuming interval (0,1) & Max_Iteration=50

>> f=inline('exp(-x)-sin(x)');   %matlab command to define function
>> df=inline('-exp(-x)-cos(x)'); %derivative of f

1)- Bisection method:
>> bisct(f,0,1,1e-6,50)
The Interval in which root lie is 5.885315e-001   5.885334e-001
Iteration Required is 20
The approx. error is 9.536743e-007

2)- Regula Falsi:
>> rfalsi(f,0,1,1e-6,50)
The Interval in which root lie is 0 5.885327e-001
Iteration Required is 50
The approx. error is 5.885327e-001

3)- Modified Regula Falsi:
>> mrfalsi(f,0,1.0,1e-6,50)
The Interval in which root lie is 5.885315e-001 5.885342e-001
Iteration Required is 23
The approx. error is 3.976230e-006

4)- Newton Raphson Method:
>> newtonrap(f,df,.2,1e-6,50)
The approx. Root is 5.885327e-001
Iteration Required is 4
b)- x- exp(-(x^2))                     assuming interval (0,1) & Max_Iteration=50
>> f=inline('x-exp(-(x^2))');          %matlab command to define function
>> df=inline('1+2*x*exp(-(x^2))');     %derivative of f

1)- Bisection method:
>> bisct(f,0,1,1e-6,50)
The Interval in which root lie is 6.529179e-001 6.529198e-001
Iteration Required is 20
The approx. error is 9.536743e-007

2)- Regula Falsi:
>> rfalsi(f,0,1.0,1e-6,50)
The Interval in which root lie is 6.529186e-001   6.534421e-001
Iteration Required is 50
The approx. error is 5.234929e-004

3)- Modified Regula Falsi:
>> mrfalsi(f,0,1.0,1e-6,50)
The Interval in which root lie is 6.529059e-001 6.529198e-001
Iteration Required is 25
The approx. error is 2.490377e-005

4)- Newton Raphson Method:
>> newtonrap(f,df,.5,1e-6,50)
The approx. Root is 6.529186e-001
Iteration Required is 4

c)- (x^3)-x-2                assuming interval (-1,2) & Max_Iteration=50
>> f=inline('(x^3)-x-2')     %matlab command to define function
>> df=inline('3*(x^2)-1');   %derivative of f

1)- Bisection method:
>> bisct(f,-1,2,1e-6,50)
The Interval in which root lie is 1.521379e+000 1.521382e+000
Iteration Required is 22
The approx. error is 2.861023e-006

2)- Regula Falsi:
>> rfalsi(f,-1,2,1e-6,50)
The Interval in which root lie is 1.521380e+000 2
Iteration Required is 50
The approx. error is 4.786203e-001
3)- Modified Regula Falsi:
>> mrfalsi(f,-1,2,1e-6,50)
The Interval in which root lie is 1.521356e+000 1.521386e+000
Iteration Required is 28
The approx. error is 6.021365e-005

4)- Newton Raphson Method:
>> newtonrap(f,df,1,1e-6,50)
The approx. Root is 1.521380e+000
Iteration Required is 6



Ques3:

For finding the the smallest positive guess for which Newton method diverges,
we can use Newton Raphson method check for various guess as the root start
diverging we can say that that is our required positive guess.


Assuming z-1e-006        % Tolerance parameter

>> f=inline('atan(x)')

f=

     Inline function:
     f(x) = atan(x)

>> df=inline('1/(1+x^2)')

df =

     Inline function:
     df(x) = 1/(1+x^2)

>> newtonrap(f,df,1,1e-6,1000)
The approx. Root is 0
Iteration Required is 5

>> newtonrap(f,df,1.1,1e-6,1000)
The approx. Root is -1.803121e-019
Iteration Required is 5
>> newtonrap(f,df,1.2,1e-6,1000)
The approx. Root is 0
Iteration Required is 6

>> newtonrap(f,df,1.3,1e-6,1000)
The approx. Root is 0
Iteration Required is 7

>> newtonrap(f,df,1.38,1e-6,1000)
The approx. Root is 0
Iteration Required is 9

>> newtonrap(f,df,1.39,1e-6,1000)
The approx. Root is 0
Iteration Required is 11

>> newtonrap(f,df,1.4,1e-6,1000)
Warning: Divide by zero.
> In newtonrap at 8
Warning: Divide by zero.
> In newtonrap at 8
The approx. Root is NaN
Iteration Required is 1000

From the above data we get
Since at 1.4 there is divide by zero error so beyond 1.4 function (‘atanx’) may
diverge.
Smallest positive guess will be 1.4.

Weitere Àhnliche Inhalte

Was ist angesagt?

3.1 derivative of a function
3.1 derivative of a function3.1 derivative of a function
3.1 derivative of a function
btmathematics
 
Approximate Integration
Approximate IntegrationApproximate Integration
Approximate Integration
Silvius
 
111015 tokyo scipy2_discussionquestionaire_i_python
111015 tokyo scipy2_discussionquestionaire_i_python111015 tokyo scipy2_discussionquestionaire_i_python
111015 tokyo scipy2_discussionquestionaire_i_python
Shohei Hido
 
Amth250 octave matlab some solutions (1)
Amth250 octave matlab some solutions (1)Amth250 octave matlab some solutions (1)
Amth250 octave matlab some solutions (1)
asghar123456
 

Was ist angesagt? (19)

Number system converted
Number system convertedNumber system converted
Number system converted
 
6. Vectors – Data Frames
6. Vectors – Data Frames6. Vectors – Data Frames
6. Vectors – Data Frames
 
3.1 derivative of a function
3.1 derivative of a function3.1 derivative of a function
3.1 derivative of a function
 
Overlap save method and overlap add method in dsp
Overlap save method and overlap add method in dspOverlap save method and overlap add method in dsp
Overlap save method and overlap add method in dsp
 
Tucker tensor analysis of Matern functions in spatial statistics
Tucker tensor analysis of Matern functions in spatial statistics Tucker tensor analysis of Matern functions in spatial statistics
Tucker tensor analysis of Matern functions in spatial statistics
 
Approximate Integration
Approximate IntegrationApproximate Integration
Approximate Integration
 
Overlap Add, Overlap Save(digital signal processing)
Overlap Add, Overlap Save(digital signal processing)Overlap Add, Overlap Save(digital signal processing)
Overlap Add, Overlap Save(digital signal processing)
 
Digital signal processing
Digital signal processingDigital signal processing
Digital signal processing
 
111015 tokyo scipy2_discussionquestionaire_i_python
111015 tokyo scipy2_discussionquestionaire_i_python111015 tokyo scipy2_discussionquestionaire_i_python
111015 tokyo scipy2_discussionquestionaire_i_python
 
Numerical_Methods_Simpson_Rule
Numerical_Methods_Simpson_RuleNumerical_Methods_Simpson_Rule
Numerical_Methods_Simpson_Rule
 
Chemistry Assignment Help
Chemistry Assignment Help Chemistry Assignment Help
Chemistry Assignment Help
 
13. Cubist
13. Cubist13. Cubist
13. Cubist
 
CLIM Fall 2017 Course: Statistics for Climate Research, Spatial Data: Models ...
CLIM Fall 2017 Course: Statistics for Climate Research, Spatial Data: Models ...CLIM Fall 2017 Course: Statistics for Climate Research, Spatial Data: Models ...
CLIM Fall 2017 Course: Statistics for Climate Research, Spatial Data: Models ...
 
Advanced matlab codigos matematicos
Advanced matlab codigos matematicosAdvanced matlab codigos matematicos
Advanced matlab codigos matematicos
 
Codecomparaison
CodecomparaisonCodecomparaison
Codecomparaison
 
Final Exam Review (Integration)
Final Exam Review (Integration)Final Exam Review (Integration)
Final Exam Review (Integration)
 
Using R Tool for Probability and Statistics
Using R Tool for Probability and Statistics Using R Tool for Probability and Statistics
Using R Tool for Probability and Statistics
 
Solution manual for theory and applications of digital speech processing lawr...
Solution manual for theory and applications of digital speech processing lawr...Solution manual for theory and applications of digital speech processing lawr...
Solution manual for theory and applications of digital speech processing lawr...
 
Amth250 octave matlab some solutions (1)
Amth250 octave matlab some solutions (1)Amth250 octave matlab some solutions (1)
Amth250 octave matlab some solutions (1)
 

Andere mochten auch

MITCOE 2011-12 conm-submission
MITCOE 2011-12 conm-submissionMITCOE 2011-12 conm-submission
MITCOE 2011-12 conm-submission
Ashutosh Katti
 
Interval Newton Method
Interval Newton MethodInterval Newton Method
Interval Newton Method
Johannes K
 
ŰąÙ…ÙˆŰČŰŽ Ù…Ű­Ű§ŰłŰšŰ§ŰȘ ŰčŰŻŰŻÛŒ - ۚ۟ێ ŰŽŰŽÙ…
ŰąÙ…ÙˆŰČŰŽ Ù…Ű­Ű§ŰłŰšŰ§ŰȘ ŰčŰŻŰŻÛŒ - ۚ۟ێ ŰŽŰŽÙ…ŰąÙ…ÙˆŰČŰŽ Ù…Ű­Ű§ŰłŰšŰ§ŰȘ ŰčŰŻŰŻÛŒ - ۚ۟ێ ŰŽŰŽÙ…
ŰąÙ…ÙˆŰČŰŽ Ù…Ű­Ű§ŰłŰšŰ§ŰȘ ŰčŰŻŰŻÛŒ - ۚ۟ێ ŰŽŰŽÙ…
faradars
 
Introduction to Numerical Methods for Differential Equations
Introduction to Numerical Methods for Differential EquationsIntroduction to Numerical Methods for Differential Equations
Introduction to Numerical Methods for Differential Equations
matthew_henderson
 
Least square method
Least square methodLeast square method
Least square method
Somya Bagai
 
Implementation of Wireless Channel Model in MATLAB: Simplified
Implementation of Wireless Channel Model in MATLAB: SimplifiedImplementation of Wireless Channel Model in MATLAB: Simplified
Implementation of Wireless Channel Model in MATLAB: Simplified
Rosdiadee Nordin
 

Andere mochten auch (18)

ExercĂ­cio mme
ExercĂ­cio mmeExercĂ­cio mme
ExercĂ­cio mme
 
MITCOE 2011-12 conm-submission
MITCOE 2011-12 conm-submissionMITCOE 2011-12 conm-submission
MITCOE 2011-12 conm-submission
 
Interval Newton Method
Interval Newton MethodInterval Newton Method
Interval Newton Method
 
Adaptive signal processing simon haykins
Adaptive signal processing simon haykinsAdaptive signal processing simon haykins
Adaptive signal processing simon haykins
 
ŰąÙ…ÙˆŰČŰŽ Ù…Ű­Ű§ŰłŰšŰ§ŰȘ ŰčŰŻŰŻÛŒ - ۚ۟ێ ŰŽŰŽÙ…
ŰąÙ…ÙˆŰČŰŽ Ù…Ű­Ű§ŰłŰšŰ§ŰȘ ŰčŰŻŰŻÛŒ - ۚ۟ێ ŰŽŰŽÙ…ŰąÙ…ÙˆŰČŰŽ Ù…Ű­Ű§ŰłŰšŰ§ŰȘ ŰčŰŻŰŻÛŒ - ۚ۟ێ ŰŽŰŽÙ…
ŰąÙ…ÙˆŰČŰŽ Ù…Ű­Ű§ŰłŰšŰ§ŰȘ ŰčŰŻŰŻÛŒ - ۚ۟ێ ŰŽŰŽÙ…
 
Euler Method using MATLAB
Euler Method using MATLABEuler Method using MATLAB
Euler Method using MATLAB
 
metode euler
metode eulermetode euler
metode euler
 
MATLAB ODE
MATLAB ODEMATLAB ODE
MATLAB ODE
 
Taylor and maclaurian series
Taylor and maclaurian seriesTaylor and maclaurian series
Taylor and maclaurian series
 
Introduction to Numerical Methods for Differential Equations
Introduction to Numerical Methods for Differential EquationsIntroduction to Numerical Methods for Differential Equations
Introduction to Numerical Methods for Differential Equations
 
Apostila de matlab
Apostila de matlabApostila de matlab
Apostila de matlab
 
Exp 5 (1)5. Newton Raphson load flow analysis Matlab Software
Exp 5 (1)5.	Newton Raphson load flow analysis Matlab SoftwareExp 5 (1)5.	Newton Raphson load flow analysis Matlab Software
Exp 5 (1)5. Newton Raphson load flow analysis Matlab Software
 
Numerical Analysis (Solution of Non-Linear Equations) part 2
Numerical Analysis (Solution of Non-Linear Equations) part 2Numerical Analysis (Solution of Non-Linear Equations) part 2
Numerical Analysis (Solution of Non-Linear Equations) part 2
 
numericai matmatic matlab uygulamalar ali abdullah
numericai matmatic  matlab  uygulamalar ali abdullahnumericai matmatic  matlab  uygulamalar ali abdullah
numericai matmatic matlab uygulamalar ali abdullah
 
NUMERICAL METHODS WITH MATLAB : bisection,mueller's,newton-raphson,false poin...
NUMERICAL METHODS WITH MATLAB : bisection,mueller's,newton-raphson,false poin...NUMERICAL METHODS WITH MATLAB : bisection,mueller's,newton-raphson,false poin...
NUMERICAL METHODS WITH MATLAB : bisection,mueller's,newton-raphson,false poin...
 
Least square method
Least square methodLeast square method
Least square method
 
Implementation of Wireless Channel Model in MATLAB: Simplified
Implementation of Wireless Channel Model in MATLAB: SimplifiedImplementation of Wireless Channel Model in MATLAB: Simplified
Implementation of Wireless Channel Model in MATLAB: Simplified
 
Matlab for Electrical Engineers
Matlab for Electrical EngineersMatlab for Electrical Engineers
Matlab for Electrical Engineers
 

Ähnlich wie Numerical methods generating polynomial

Natural and Clamped Cubic Splines
Natural and Clamped Cubic SplinesNatural and Clamped Cubic Splines
Natural and Clamped Cubic Splines
Mark Brandao
 
Fourier series example
Fourier series exampleFourier series example
Fourier series example
Abi finni
 
Amth250 octave matlab some solutions (2)
Amth250 octave matlab some solutions (2)Amth250 octave matlab some solutions (2)
Amth250 octave matlab some solutions (2)
asghar123456
 
Solution of non-linear equations
Solution of non-linear equationsSolution of non-linear equations
Solution of non-linear equations
ZunAib Ali
 
Incorporate the SOR method in the multigridTest-m and apply the multig.pdf
Incorporate the SOR method in the multigridTest-m and apply the multig.pdfIncorporate the SOR method in the multigridTest-m and apply the multig.pdf
Incorporate the SOR method in the multigridTest-m and apply the multig.pdf
aartechindia
 
Mit6 094 iap10_lec03
Mit6 094 iap10_lec03Mit6 094 iap10_lec03
Mit6 094 iap10_lec03
Tribhuwan Pant
 
Chapter 1 (math 1)
Chapter 1 (math 1)Chapter 1 (math 1)
Chapter 1 (math 1)
Amr Mohamed
 

Ähnlich wie Numerical methods generating polynomial (20)

Natural and Clamped Cubic Splines
Natural and Clamped Cubic SplinesNatural and Clamped Cubic Splines
Natural and Clamped Cubic Splines
 
Fourier series example
Fourier series exampleFourier series example
Fourier series example
 
Applied numerical methods lec10
Applied numerical methods lec10Applied numerical methods lec10
Applied numerical methods lec10
 
05_AJMS_332_21.pdf
05_AJMS_332_21.pdf05_AJMS_332_21.pdf
05_AJMS_332_21.pdf
 
Amth250 octave matlab some solutions (2)
Amth250 octave matlab some solutions (2)Amth250 octave matlab some solutions (2)
Amth250 octave matlab some solutions (2)
 
Non linearequationsmatlab
Non linearequationsmatlabNon linearequationsmatlab
Non linearequationsmatlab
 
Solution of non-linear equations
Solution of non-linear equationsSolution of non-linear equations
Solution of non-linear equations
 
Incorporate the SOR method in the multigridTest-m and apply the multig.pdf
Incorporate the SOR method in the multigridTest-m and apply the multig.pdfIncorporate the SOR method in the multigridTest-m and apply the multig.pdf
Incorporate the SOR method in the multigridTest-m and apply the multig.pdf
 
Matlab polynimials and curve fitting
Matlab polynimials and curve fittingMatlab polynimials and curve fitting
Matlab polynimials and curve fitting
 
Intro to Matlab programming
Intro to Matlab programmingIntro to Matlab programming
Intro to Matlab programming
 
AJMS_389_22.pdf
AJMS_389_22.pdfAJMS_389_22.pdf
AJMS_389_22.pdf
 
Multirate sim
Multirate simMultirate sim
Multirate sim
 
Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Applie...
Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Applie...Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Applie...
Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Applie...
 
Mit6 094 iap10_lec03
Mit6 094 iap10_lec03Mit6 094 iap10_lec03
Mit6 094 iap10_lec03
 
2. Fixed Point Iteration.pptx
2. Fixed Point Iteration.pptx2. Fixed Point Iteration.pptx
2. Fixed Point Iteration.pptx
 
Chapter 1 (math 1)
Chapter 1 (math 1)Chapter 1 (math 1)
Chapter 1 (math 1)
 
curve fitting or regression analysis-1.pptx
curve fitting or regression analysis-1.pptxcurve fitting or regression analysis-1.pptx
curve fitting or regression analysis-1.pptx
 
Application of derivative
Application of derivativeApplication of derivative
Application of derivative
 
5 numerical analysis
5 numerical analysis5 numerical analysis
5 numerical analysis
 
SIAM - Minisymposium on Guaranteed numerical algorithms
SIAM - Minisymposium on Guaranteed numerical algorithmsSIAM - Minisymposium on Guaranteed numerical algorithms
SIAM - Minisymposium on Guaranteed numerical algorithms
 

Mehr von Department of Telecommunications, Ministry of Communication & IT (INDIA) (8)

Workshop proposal
Workshop proposalWorkshop proposal
Workshop proposal
 
Feeding system for disabled report
Feeding system for disabled reportFeeding system for disabled report
Feeding system for disabled report
 
Design ideas mayank
Design ideas mayankDesign ideas mayank
Design ideas mayank
 
Pdpm,mayank awasthi,jabalpur,i it kanpur, servo motor,keil code
Pdpm,mayank awasthi,jabalpur,i it kanpur, servo motor,keil codePdpm,mayank awasthi,jabalpur,i it kanpur, servo motor,keil code
Pdpm,mayank awasthi,jabalpur,i it kanpur, servo motor,keil code
 
Analogic
AnalogicAnalogic
Analogic
 
jacobi method, gauss siedel for solving linear equations
jacobi method, gauss siedel for solving linear equationsjacobi method, gauss siedel for solving linear equations
jacobi method, gauss siedel for solving linear equations
 
An Introduction To Speech Recognition
An Introduction To Speech RecognitionAn Introduction To Speech Recognition
An Introduction To Speech Recognition
 
Numerical Methods Solving Linear Equations
Numerical Methods Solving Linear EquationsNumerical Methods Solving Linear Equations
Numerical Methods Solving Linear Equations
 

KĂŒrzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

KĂŒrzlich hochgeladen (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

Numerical methods generating polynomial

  • 1. EC 313: Numerical Methods Mayank Awasthi MATLAB Assignment – 1 M. Tech, IIT Roorkee (Communication Systems) Ques1: MATLAB routine which finds the coefficients of Lagrange polynomial is below: %Implementing Lagrange Interpolating Formula function l = lagranp(x,y) N = length(x)-1; %the degree of polynomial l = 0; for m = 1:N + 1 P = 1; for k = 1:N + 1 %Multiplication of two polynomials corresponds to convolution of their coefficients. if k ~= m %”linconv” function created by own. P = linconv(P,[1 -x(k)])/(x(m)-x(k)); end end l = l + y(m)*P; %Lagrange polynomial coefficients end MATLAB routine for Linear Convolution: %Linear convolution of sequence using formula function s=linconv(x,h) N=length(x); M=length(h); for n=1:N+M-1 s(n)=0; for k=1:M a=n-k; if a<0 y(k)=0; elseif k>N y(k)=0; elseif a<M y(k)=x(k)*h(a+1); else y(k)=0; end s(n)=s(n)+y(k); end end
  • 2. MATLAB routine which finds the coefficients of Newton polynomial is below: %Implementing the Newton divided difference formula function n = newtonp(x,y) N = length(x)-1; f = zeros(N+1,1); f(1:N+1,1) = y; for k = 2:N + 1 for m = 1: N + 2 - k %Divided Difference f(m,k) = (f(m + 1,k - 1) - f(m,k - 1))/(x(m + k - 1)- x(m)); end end a = f(1,:); n = a(N+1); for k =N:-1:1 %Nested multiplication form for better computation efficiency %a_k=f(x0,x1,


.xk) n = [n a(k)] - [0 n*x(k)]; %f(x)*(a_k*(x - x(k - 1))+a_k – 1) end >> x=[1 3 4]; Calculating the polynomial using both routines for >> y=[1 27 64]; given data. >> coefflag = lagranp(x,y) coefflag = 8.0000 -19.0000 12.0000 So required polynomial we get 8x2 – 19x + 12 >> coeffnew = newtonp(x,y) coeffnew = 8 -19 12
  • 3. Ques2: a)- For Straight Line: deg(Polynomial)=1 So using Newton or Lagrange routine we can get the coeff. Of polynomial as below: >> x= [1970 1990] ; >> y= [3707475887 5281653820] ; >> nc=newtonp(x,y) nc = 1.0e+011 * 0.0008 -1.5135 >> v0=polyval(nc,1980) v0 = % 1980 Population using straight line 4.4946e+009 b)- For parabola: deg(Polynomial)=2 >> x= [1960 1970 1990]; >> y= [3039585530 3707475887 5281653820]; >> nc= newtonp(x,y) nc = 1.0e+012 * 0.0000 -0.0015 1.4063 >> v1=polyval(nc,1980) % 1980 Population parabola v1 = 4.4548e+009 c)- For cubic curve deg(Polynomial) >> x= [1960 1970 1990 2000]; >> y= [3039585530 3707475887 5281653820 6079603571]; >> nc=newtonp(x,y) nc = 1.0e+013 * -0.0000 0.0000 -0.0107 7.0777 >> v3=polyval(nc,1980) % 1980 Population using cubic curve v3 =
  • 4. 4.4729e+009 Ques4: MATLAB routine which computes the divide difference coefficient is below: %matalb script for computing dividedifference coefff. function ddc = dividediff(x,y) N = length(x)-1; f = zeros(N+1,1); f(1:N+1,1) = y; for k = 2:N + 1 for m = 1: N + 2 - k %Divided Difference f(m,k) = (f(m + 1,k - 1) - f(m,k - 1))/(x(m + k - 1)- x(m)); end ddc=f(1,:); end end Calculating the divide difference coefficients. >> x=[1 2 4 5 7 9]; >> y=[2 3 8 6 14 6]; >> divide_diff_coeff = dividediff(x,y) divide_diff_coeff = 2.0000 1.0000 0.5000 -0.5000 0.2000 -0.0518 Now Calculating the polynomial using Newton method routine used in Ques1. >> x=[1 2 4 5 7 9]; >> y=[2 3 8 6 14 6]; >> nc= newtonp(x,y) nc = -0.0518 1.1839 -9.7875 35.6018 -53.4464 28.5000 Required Polynomial is -0.0518x5 + 1.1839x4 -9.7875x3 + 35.6018x2-53.4464x +28.5000 v0=p(1.5), v1=p(3.7), v2=p(6.6), v3=p(9.5) >> v0=polyval(nc,1.5) >> v2=polyval(nc,6.6) v0 = v2 = 1.0020 10.6441 >> v1=polyval(nc,3.7) >> v3=polyval(nc,9.5) v1 = v3 =
  • 5. 8.3474 -21.6454 Plotting Graph >> x=[1 2 4 5 7 9]; >> y=[2 3 8 6 14 6]; >> nc= newtonp(x,y) nc = -0.0518 1.1839 -9.7875 35.6018 -53.4464 28.5000 >> m=[0:10]; >> n=polyval(nc,m) n= 28.5000 2.0000 3.0000 7.6286 8.0000 6.0000 7.0714 14.0000 20.7000 6.0000 -72.5714 >> plot(m,n)
  • 6. Ques5 : Bisection Method : %Bisection method to solve Non-linear equations. function [x] = bisct(f,a,b,z,MaxIter) fa = feval(f,a); fb = feval(f,b); if fa*fb > 0 error('We must have f(a)f(b)<0!'); end for k = 1:MaxIter xx(k) = (a + b)/2; fx =feval(f,xx(k)); err = (b-a)/2; if abs(err)<z break; elseif fx*fa > 0 a = xx(k); fa = fx; else b = xx(k); end x = xx(k); end fprintf('The Interval in which root lie is %d %dn',a,b) fprintf('Iteration Required is %dn',k) fprintf('The approx. error is %dn',err) end Regula Falsi Method : %Implementing Regula Falsi algorithm. function [] = falsp(f,a,b,z,MaxIter) fa =feval(f,a); fb=feval(f,b); if fa*fb > 0 error('We must have f(a)f(b)<0!'); end for k = 1: MaxIter xx(k) = (a*fb-b*fa)/(fb-fa); fx = feval(f,xx(k)); err = max(abs(xx(k) - a),abs(b - xx(k))); if err<z break; elseif fx*fa > 0 a = xx(k); fa = fx; else b = xx(k); fb = fx; end end x = xx(k); fprintf('The Interval in which root lie is %d %dn',a,b) fprintf('Iteration Required is %dn',k) fprintf('The approx. error is %dn',err) end
  • 7. Modified Regula Falsi Method : %Implementing Modified Regula Falsi algorithm function [] = mrfalsp(f,a,b,z,MaxIter) fa =feval(f,a); fb=feval(f,b); F=fa; G=fb; if fa*fb > 0 error('We must have f(a)f(b)<0!'); end xx(1)=a/2; for k = 1: MaxIter xx(k+1) = (a*G-b*F)/(G-F); fx = feval(f,xx(k+1)); err = max(abs(xx(k) - a),abs(b - xx(k))); if err<z break; end if fx*fa > 0 a = xx(k+1); fa = fx; elseif feval(f,xx(k))*feval(f,xx(k+1))>0 F=F/2; end if fx*fa < 0 b = xx(k+1); fb = fx; elseif feval(f,xx(k))*feval(f,xx(k+1))>0 G=G/2; end end fprintf('The Interval in which root lie is %d %dn',a,b) fprintf('Iteration Required is %dn',k) fprintf('The approx. error is %dn',err) end
  • 8. Newton Raphson Meyhod : %Implementing Newton Raphson Method function [] = newton(f,df,x0,z,MaxIter) xx(1) = x0; fx = feval(f,x0); for k = 1: MaxIter if ~isnumeric(df) dfdx = feval(df,xx(k));end dx = -fx/dfdx; xx(k+1) = xx(k)+dx; fx = feval(f,xx(k + 1)); a= xx; if abs(dx) < z break; end end l=length(a); fprintf('The approx. Root is %dnn',x); fprintf('Iteration Required is %dn',k) end Now calculating the roots of given polynomials a)- exp(-x)-sin(x) assuming interval (0,1) & Max_Iteration=50 >> f=inline('exp(-x)-sin(x)'); %matlab command to define function >> df=inline('-exp(-x)-cos(x)'); %derivative of f 1)- Bisection method: >> bisct(f,0,1,1e-6,50) The Interval in which root lie is 5.885315e-001 5.885334e-001 Iteration Required is 20 The approx. error is 9.536743e-007 2)- Regula Falsi: >> rfalsi(f,0,1,1e-6,50) The Interval in which root lie is 0 5.885327e-001 Iteration Required is 50 The approx. error is 5.885327e-001 3)- Modified Regula Falsi: >> mrfalsi(f,0,1.0,1e-6,50) The Interval in which root lie is 5.885315e-001 5.885342e-001 Iteration Required is 23 The approx. error is 3.976230e-006 4)- Newton Raphson Method: >> newtonrap(f,df,.2,1e-6,50) The approx. Root is 5.885327e-001 Iteration Required is 4
  • 9. b)- x- exp(-(x^2)) assuming interval (0,1) & Max_Iteration=50 >> f=inline('x-exp(-(x^2))'); %matlab command to define function >> df=inline('1+2*x*exp(-(x^2))'); %derivative of f 1)- Bisection method: >> bisct(f,0,1,1e-6,50) The Interval in which root lie is 6.529179e-001 6.529198e-001 Iteration Required is 20 The approx. error is 9.536743e-007 2)- Regula Falsi: >> rfalsi(f,0,1.0,1e-6,50) The Interval in which root lie is 6.529186e-001 6.534421e-001 Iteration Required is 50 The approx. error is 5.234929e-004 3)- Modified Regula Falsi: >> mrfalsi(f,0,1.0,1e-6,50) The Interval in which root lie is 6.529059e-001 6.529198e-001 Iteration Required is 25 The approx. error is 2.490377e-005 4)- Newton Raphson Method: >> newtonrap(f,df,.5,1e-6,50) The approx. Root is 6.529186e-001 Iteration Required is 4 c)- (x^3)-x-2 assuming interval (-1,2) & Max_Iteration=50 >> f=inline('(x^3)-x-2') %matlab command to define function >> df=inline('3*(x^2)-1'); %derivative of f 1)- Bisection method: >> bisct(f,-1,2,1e-6,50) The Interval in which root lie is 1.521379e+000 1.521382e+000 Iteration Required is 22 The approx. error is 2.861023e-006 2)- Regula Falsi: >> rfalsi(f,-1,2,1e-6,50) The Interval in which root lie is 1.521380e+000 2 Iteration Required is 50 The approx. error is 4.786203e-001
  • 10. 3)- Modified Regula Falsi: >> mrfalsi(f,-1,2,1e-6,50) The Interval in which root lie is 1.521356e+000 1.521386e+000 Iteration Required is 28 The approx. error is 6.021365e-005 4)- Newton Raphson Method: >> newtonrap(f,df,1,1e-6,50) The approx. Root is 1.521380e+000 Iteration Required is 6 Ques3: For finding the the smallest positive guess for which Newton method diverges, we can use Newton Raphson method check for various guess as the root start diverging we can say that that is our required positive guess. Assuming z-1e-006 % Tolerance parameter >> f=inline('atan(x)') f= Inline function: f(x) = atan(x) >> df=inline('1/(1+x^2)') df = Inline function: df(x) = 1/(1+x^2) >> newtonrap(f,df,1,1e-6,1000) The approx. Root is 0 Iteration Required is 5 >> newtonrap(f,df,1.1,1e-6,1000) The approx. Root is -1.803121e-019 Iteration Required is 5
  • 11. >> newtonrap(f,df,1.2,1e-6,1000) The approx. Root is 0 Iteration Required is 6 >> newtonrap(f,df,1.3,1e-6,1000) The approx. Root is 0 Iteration Required is 7 >> newtonrap(f,df,1.38,1e-6,1000) The approx. Root is 0 Iteration Required is 9 >> newtonrap(f,df,1.39,1e-6,1000) The approx. Root is 0 Iteration Required is 11 >> newtonrap(f,df,1.4,1e-6,1000) Warning: Divide by zero. > In newtonrap at 8 Warning: Divide by zero. > In newtonrap at 8 The approx. Root is NaN Iteration Required is 1000 From the above data we get Since at 1.4 there is divide by zero error so beyond 1.4 function (‘atanx’) may diverge. Smallest positive guess will be 1.4.