Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

MatLab 4

Ameer Muavia Me191076


Code 1:
%% Numerical Analysis
% Lab Session 04 - In this Lab Session we will find out roots of equation
% using NWRP METHOD
% Name: Ameer Muavia
%Roll no: me191076
%% Clearing
clc;
clear all;
close all;

err=1;
x_old=0; %initial guess

while abs(err)>=0.0000001

f=exp(x_old)-3*(x_old)^2;
df=-exp(-x_old)-6*(x_old);
x_new=x_old-(f/df)
err=exp(x_new)-3*(x_new)^2;
x_old=x_new;
fprintf('xnew = %d ; err = %.7f\n',x_new,err);
end

Result:
Code 2 :

%% Numerical Analysis
% Lab Session 04 - In this Lab Session we will find out roots of equation
% using SECANT METHOD
% Name: Ameer Muavia
%Roll no: me191076
%% Clearing
clc; clear all; close all;
x0=0;x1=1; %initial guess
err=1;
counter=0;
while abs(err)>0.0000001
fx0=exp(x0)-3*x0^2;
fx1=exp(x1)-3*x1^2;
x2=x1-(((fx1)*(x0-x1))/(fx0-fx1))
err=exp(x2)-3*x2^2
x0=x1;
x1=x2;
counter=counter+1;
fprintf('x2 = %d ; err = %.10f\n',x2,err);

end

Result:

You might also like