Showing posts with label Codes. Show all posts
Showing posts with label Codes. Show all posts

FreeMat Code: Sample hen's method or raunge-kutta Method

x1=0;
y1=1;
h=0.05;
xf=0.25;
printf('x   y\n');
    while(x1<=xf)
    s1=-(x1*y1);
    x2=x1+h;
    y2=y1+(h*s1);
    s2=-(x2*y2);
    y2=y1+h*(s1+s2)/2;
    printf('%f %f\n',x1,y1);
    x1=x2;
    y1=y2;
    
end



For more,
Link:- https://github.com/ananya-datta/FreeMat-MATLAB-Programs

FreeMat code: Sample Newton-Rapson Method

x0=2.5;
epsilon=0.001;
delta=0.001;
n=5;
for (i=1:n)
    f0=(x0*sin(x0))+cos(x0);
    fd0=x0*cos(x0);
    
    x1=x0-(f0/fd0);
    if((x1-x0)/x1)<epsilon
        x1
        fx=(x1*x1)-25;
        fx
        i
    end
    x0=x1;
end


For more,
Link:- https://github.com/ananya-datta/FreeMat-MATLAB-Programs

FreeMat Code: Sample False position Method

x0=2;
x1=8;
e=0.001;
n=10;
f0=(x0*x0*x0)-(2*x0)-5;
f1=(x1*x1*x1)-(2*x1)-5;
j=1;
k=1;
x5=x1;
printf('iter   x0         x1         x2           f(x0)          f(x1)         f(x2)            error        \n'); 
 
 for (i=1:n)
    x2=((x0*f1)-(x1*f0))/(f1-f0);
    f2=(x2*x2*x2)-(2*x2)-5;
    error=x5-x2;
       printf('%d    %f   %f   %f     %f     %f      %f       %f  \n',i,x0,x1,x2,f0,f1,f2,error);     
  if(abs(f2)<e)     
     break;
  end
 if(f2<0 && f0>0 || f2>0 && f0<0)
        x1=x2;
        f1=f2;
        x5=x1;
    else
        x0=x2;
        f0=f2;
        x5=x0;
    end
     x3(j)=x2;
    j=j+1;
    x4(k)=error;
    k=k+1;
end
Root=x2
plot(x4,'r',x3,'g')



For more,
Link:- https://github.com/ananya-datta/FreeMat-MATLAB-Programs

FreeMat Code: Sample Gauss Sidel Method

e=0.000001;
maxit=100;
n=3;
a=[20 1 -2 17;3 20 -1 -18;2 -3 20 25]
printf('Iter      x        y          z\n');
printf('-------------------------------------\n');
                        for(i=1:n)
                        x(i)=0;
                        end
                       for(iter=1:maxit)
                  big=0;
        for(i=1:n)
            sum=0;
            for(j=1:n)
                if(j~=i)
                    sum=sum+a(i,j)*x(j);
                 end
            end
                 temp=(a(i,n+1)-sum)/a(i,i);
                 relerror=abs((x(i)-temp)/temp);
                 
                 if(relerror>big)
                     big=relerror;
                     x(i)=temp;
                 end
             end
        if(big<=e)
            for(i=1:n)
                x(i); 
             end 
            break;
          end
        if(mod(iter+2,3)==0)                 
               printf('%d    %f   %f   %f\n',iter/3,x(1),x(2),x(3));
              end
          end
     for(i=1:n)
         x(i);
       end       
      printf('\n\n x=%f \t           y=%f \t              z=%f\n',x(1),x(2),x(3));
      

    
For more,
Link:- https://github.com/ananya-datta/FreeMat-MATLAB-Programs