smziaurrashid icon

Finding Roots of an Equation

smziaurrashid | PRO | 12/25/16 06:22:46 PM UTC | 0 ⭐ | 7665 👁️ | Never ⏰ | []
MatLab |

3.24 KB

|

None

|

0 👍

/

0 👎

clc;
clear;
 
% make a function file eee18.m %
% function[y]=eee18(x)
y=x^3+2*x-5;
end %
 
% make another function file eee19.m %
% function[y]=eee19(x)
y=3*x+2;
end %
 
disp('© ET-153071™')
disp('>> Choose a Method');
disp('1. Bisection');
disp('2. False-position');
disp('3. Newton-Raphson');
disp('4. All in One');
answer=input('>> Choosen Option: ');
if answer==1
x1=input('- Enter the value of x1:');
x2=input('- Enter the value of x2:');
y1=eee18(x1);
y2=eee18(x2);
 if (y1*y2)<0
    disp('Initial values are correct');
         for n=1:1:100
        y1=eee18(x1);    
        y2=eee18(x2);
        xm=(x1+x2)/2; 
        y0=eee18(xm);
        
          if y0==0 
              break
          end
          if y1*y0>0
            x1=xm;            
          else  x2=xm;
           
          end
         end
        M=xm;
    fprintf('Bisection Root: %f\n', M);
    else 
    disp('Initial values are incorrect'); 
 end
         
 
elseif answer==2
    
x1=input('- Enter the value of x1:');
x2=input('- Enter the value of x2:');
y1=eee18(x1);
y2=eee18(x2);
if (y1*y2)<0
    disp('Initial values are correct');
 
     for n=1:1:10
        y1=eee18(x1); 
        y2=eee18(x2);
        xr=x1-(y1*(x2-x1))/(y2-y1);
        y0=eee18(xr);
   if y0==0 
   break
   elseif abs(y0)<0.00001
       break
   elseif y1*y0>0;
            x2=xr;
          
   else
       x1=xr;
   end
     end
    N=xr;
    fprintf('False Position Root: %f\n', N);
    else
    disp('Initial values are incorrect');
end
    
elseif answer==3
x1=input('Enter the value of x1:');
y1=eee18(x1);
y2=eee19(x1);
if (y1*y2)<0
    disp('Initial values are correct')
     for n=1:1:100
        y1=eee18(x1); 
        y2=eee19(x1);
        xq=x1-(y1/y2); 
        y0=eee18(xq);
        
        if y0==0
        break
        end
        x1=xq;
     end 
    P=xq;
    fprintf('Newton Root: %f\n', P);
    else
    disp('Initial values are incorrect');
end
 
elseif answer==4
x1=input('- Enter the value of x1:');
x2=input('- Enter the value of x2:');
y1=eee18(x1);
y2=eee18(x2);
 if (y1*y2)<0
    disp('Initial values are correct');
    
    for n=1:1:100
        y1=eee18(x1);    
        y2=eee18(x2);
        xm=(x1+x2)/2; 
        y0=eee18(xm);
        
          if y0==0 
              break
          end
          if y1*y0>0
            x1=xm;            
          else  x2=xm;
           
          end
    end
        M=xm;
    
        for n=1:1:10
        y1=eee18(x1); 
        y2=eee18(x2);
        xr=x1-(y1*(x2-x1))/(y2-y1);
        y0=eee18(xr);
   if y0==0 
   break
   elseif abs(y0)<0.00001
       break
   elseif y1*y0>0;
            x2=xr;
          
   else
       x1=xr;
   end
       end
    N=xr;
        
    for n=1:1:100
        y1=eee18(x1); 
        y2=eee19(x1);
        xq=x1-(y1/y2); 
        y0=eee18(xq);
        
        if y0==0
        break
        end
        x1=xq;
     end 
    P=xq;
    Q=[M,N,P];
    disp('Bisection False-Position Newton-Raphson')
    disp('---------------------------------------')
    disp(Q)
    
 else
     disp('Initial values are incorrect');
 end
    
 
else
    disp('Please, choose any option!');
end

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎