1. Numerical Analysis

Fixed-point iteration. 

 

%%Finding roots of an equation by Fixed-point iteration.
%%
close all
clear all
clc
format long
x=-5:0.001:5;
y=(1-x).^(1/3);
plot(x,y)

x(1)=0.5;     %Initialization.
tol=10^-4;  %Tolerence.
k=100;      %Number of loops taken.
f=@(x) (1-x).^(1/3);   

for i=1:k
    x(i+1)=f(x(i));
    if f(x(i))==0
        break;
    elseif abs(x(i)-f(x(i)))<tol
        break
    end
end
x(1:i+1)'%Gives roots.
%(1:i+1) is used to show the roots and to avoid others value of x.

========================================================================


Newton-Raphson Method.

clear all
close all
clc
format long
x=-2:0.0001:2;
y=x.^3+x-1;
plot(x,y)
f=@(x) x.^3+x-1;
fd=@(x) 3*x.^2+1;
k=100;
tol=10^-5;
x(1)=3;
for i=1:k
    if fd(x(i))==0
        disp('First derivetive=0')
        break;
    end
    x(i+1)=x(i)-((f(x(i)))./(fd(x(i))));
  
    if f(x(i+1))==0
        break      
    elseif abs(x(i)-x(i+1))<tol
        break;
    end
end
x(1:i+1)'
i

x(1:i+1)'%Gives roots.
%(1:i+1) is used to show only the roots and to leave others value of x.

 

CT Question 01: 

Sketch the graph of f(x)=2x^3-6x-1.  Taking interval length one and using the Bi-section Method to find the root f(x)=2x^3-6x-1correct to 6 decimal places.

clear all;
close all;
clc;
format long
x=-3:0.0001:3;
f= 2*x.^3-6*x-1;
plot(x,f)
grid on

a=1;
b=2;
p=6;
f=@(x) 2*x.^3-6*x-1;
fa=f(a);
fb=f(b);
if sign(fa*fb)<0
    fprintf('root exits');
else
    fprintf('root not exits');
end

while (b-a)/2>0.5*10^(-p)
    c=(a+b)/2
    fc=f(c);
    if fc==0
        fprintf('%d is the root',c);
        break;
    else
        if sign(fa*fc)<0
            b=c;
            fb=fc;
        else
            a=c;
            fa=fc;
        end
    end
end
xc=(a+b)/2;

No comments:

Post a Comment