Showing posts with label Matlab. Show all posts
Showing posts with label Matlab. Show all posts

Wednesday, May 9, 2012

Newtons Forward Interpolation Using Matlab


function fp=forward_interpolation(x,y,p)
n=length(x);
for i=1:n
    diff(i,1)=y(i);
end
for j=2:n
    for i=1:n-j+1
        diff(i,j)=diff(i+1,j-1)-diff(i,j-1);
    end
end
answer=y(1);
h=x(2)-x(1);
s=(p-x(1))/h;
for i=1:n-1
    term=1;
    for j=1:i
        term=term*(s+j-1)/j;
    end
    answer=answer+term*diff(1,i+1);
end
fp=answer;

Arguments sent to the function are:- lagrange(xdataset,ydataset,interpolating point)

Newtons Backward Interpolation Using Matlab


function bp=backward_interpolation(x,y,p)
n=length(x);
for i=1:n
    diff(i,1)=y(i);
end
for j=2:n
    for i=n:-1:j
        diff(i,j)=diff(i,j-1)-diff(i-1,j-1);
    end
end
answer=y(n);
h=x(n)-x(n-1);
s=(p-x(n))/h;
for i=1:n-1
    term=1;
    for j=1:i
        term=term*(s+j-1)/j;
    end
    answer=answer+term*diff(n,i+1);
end
bp=answer;

Arguments sent to the function are:- lagrange(xdataset,ydataset,interpolating point)

Lagrange Interpolating Polynomial Using Matlab


function lg=lagrange(x,y,lp)
n=length(x);
a=0;
   for i=1:n    
       p=1;
       q=1;
       for j=1:n
         if (i~=j)
            p=p*(lp-x(j));
            q=q*(x(i)-x(j));
         end
       end
      a=a+(p/q)*y(i);
   end
lg=a;


Arguments sent to the function are:- lagrange(xdataset,ydataset,interpolating point)

Trapezoidal Rule Using Matlab


function[y]=f(x)
y=1/(1+(x*x));

Save the above function as f.m.



function[sum]=trap(a,b,n)
h=(b-a)/n;
for i=1:n+1
   x(i)=a;
   a=a+h;
end
sum=0;
sum=sum+f(x(1))+f(x(n+1));
for i=2:n
   sum=sum+2*f(x(i));
end
sum=sum*(h/2);



Arguments sent to the function are:- trap(lower bound,upper bound,number of sub intervals)

Simpsons 1/3rd Rule Using Matlab



function[y]=f(x)
y=1/(1+(x*x));

Save the above function as f.m.


function[sum]=simpson(a,b,n)
sum=0;
h=0;
h=(b-a)/n;
for i=1:n+1
   x(i)=a;
   a=a+h;
end
sum=sum+f(x(1))+f(x(n+1));
for i=2:2:n
   sum=sum+4*f(x(i));
end
for i=3:2:n
   sum=sum+2*f(x(i));
end
sum=sum*(h/3);

Arguments sent to the function are:- simpson(lower bound,upper bound,number of sub intervals)

Regula Falsi Method Using Matlab


function[y]=f(x)
y=(x*x*x)-(2*x)-5;

Save the above function as f.m.


function regula_falsi(a,b)
if(f(b)<f(a))
    m=a;
    a=b;
    b=m;
end
if((f(a)>0 && f(b)>0)||(f(a)<0 && f(b)<0))
    fprintf('\nNo root present!!!\n');
    return;
end
c=1;
while((abs(a-b)>0.001)&&c~=50)
    c=c+1;
    m=(a*f(b)-b*f(a))/(f(b)-f(a));
    if(f(m)>0)
        b=m;
    end
    if(f(m)<0)
        a=m;
    end
end
  fprintf('\nRoot=%f\n',m);

Arguments sent to the function are:- bisection(lower approx root,upper approx root)

Bisection Method using Matlab



function[y]=f(x)
y=(x*x*x)-(2*x)-5;

Save the above function as f.m.



function bisection(a,b)
if(f(b)<f(a))
    m=a;
    a=b;
    b=m;
end
if((f(a)>0 && f(b)>0)||(f(a)<0 && f(b)<0))
    fprintf('\nNo root present!!!\n');
    return;
end
c=1;
while((abs(a-b)>0.001)&&c~=50)
    c=c+1;
    m=(a+b)/2;
    if(f(m)>0)
        b=m;
    end
    if(f(m)<0)
        a=m;
    end
end
  fprintf('\nRoot=%f\n',m);

Arguments sent to the function are:- bisection(lower approx root,upper approx root)