Lab Final
Lab Final
ID : 17201012
SOLVE :
Set A Q 2:
% 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
ref = 12;
function FDD(x,i)
x2 = x + delta_x;
%f(x) = 6e^(−3x)+3
FDD = (y2-y1)/delta_x;
disp(['The value of FDD is: ', num2str(FDD), ' For X', num2str(i)]);
endfunction
% 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
ref =12;
f = @(x) (4*x^3+7*x+3-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
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
sig=1;
endif
sig=2;
endif
sig=3;
endif
sig=4;
endif
sig=5;
endif
sig=6;
endif
matrix(iter,4) = sig;
x_old = x_new;
iter=iter+1;
endwhile
disp(matrix)
ScreenShots: