0% found this document useful (0 votes)
47 views8 pages

Lab Final

The document contains the code to solve two questions: 1) Use forward divided difference approximation to calculate the derivative of f(x) = 6e^-3x+3 at x0=2.12, x1, x2 with a step size of 2, and plot the results. 2) Use the Newton-Raphson method to estimate the root of 4x^3+7x+3 = e^x with an initial guess of 3 over 3 iterations, and display the results in a 10x4 matrix showing the iteration number, root, relative approximate error, and number of significant digits.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views8 pages

Lab Final

The document contains the code to solve two questions: 1) Use forward divided difference approximation to calculate the derivative of f(x) = 6e^-3x+3 at x0=2.12, x1, x2 with a step size of 2, and plot the results. 2) Use the Newton-Raphson method to estimate the root of 4x^3+7x+3 = e^x with an initial guess of 3 over 3 iterations, and display the results in a 10x4 matrix showing the iteration number, root, relative approximate error, and number of significant digits.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Name : Rashik Rahman

ID : 17201012

SOLVE :

Set A Q 2:

%Author : Rashik Rahman(17201012)

% Question 2, Set A : Use forward divided difference approximation of the first derivative of f(x) =
6e−3x+3 to calculate the

%derivative at x0=2.12, x1, x2 with a step size of 2. Print the FDD result

%and use plot function to display the values of x (i.e. x0, x1, x2) and their corresponding

%values of y by x-axis and y-axis, respectively.

ref = 12;

function FDD(x,i)

delta_x = 2; % Step size

x2 = x + delta_x;

%f(x) = 6e^(−3x)+3

y2 = 6*e^(-3*x2)+3; % Here y2 is f(x+delta_x)

y1 = 6*e^(-3*x)+3; % Here y1is f(x)

FDD = (y2-y1)/delta_x;

disp(['The value of FDD is: ', num2str(FDD), ' For X', num2str(i)]);
endfunction

FDD(2.12,0) %Function call of x0

FDD(4.12,1) %Function call of x1

FDD(6.12,2) %Function call of x2

% Plotting x and y

x = 2.12:2:6.12;

y = 6*e.^(-3*x)+3;

plot(x,y)

Set B Q 2:

% Question 2, Set B : Use Newton-Raphson method to estimate the root of 4x3+7x+3 = e^x. Conduct 3
iterations with an initial guess 3.

%Show the result in a 10×4 matrix that contains 4 columns such as: Iteration No., Root (xi), Absolute
relative approximate error (|Ɛa|), and

%No. of significant digits.

ref =12;

f = @(x) (4*x^3+7*x+3-e^x);

fprime = @(x) (12*x^2+7-e^x);

iter = 2;

x_old = 3;
matrix = zeros(10,4);

matrix(1,1) = 0;

matrix(1,2) = 3;

matrix(1,3) = NaN;

matrix(1,4) = NaN;

while iter<=11

x_new = x_old - f(x_old)/fprime(x_old);

matrix(iter,1) = iter-1;

matrix(iter,2) = x_new;

eror = abs((abs(x_new-x_old)/x_new)*100);

matrix(iter,3) = eror;

sig = 0;

if eror>5

sig=0;

endif

if eror>0.5 && eror<=5

sig=1;

endif

if eror>0.05 && eror<=0.5

sig=2;

endif

if eror>0.005 && eror<=0.05

sig=3;
endif

if eror>0.0005 && eror<=0.005

sig=4;

endif

if eror>0.00005 && eror<=0.0005

sig=5;

endif

if eror>0.000005 && eror<=0.00005

sig=6;

endif

matrix(iter,4) = sig;

x_old = x_new;

iter=iter+1;

endwhile

disp(matrix)
ScreenShots:

You might also like