Question

3. Write a code to find 3 roots of the function f(x)-2x3-4x2-22x+24 for the interval -5, 5] considering the following methods
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:-

MAT LAB CODE:-

. Execute demo-m STDIN 1 f @(x) 2%.3-44x.2-22*x 24; + = 2 df (x) 6*x.2-8*x - 22; 31inspace(-5,5) 4. 5 % plotting Function a32 function xrBisection(f,xl,xu) 33 % fis function 34 % XL ,xu are initial Guesses 35 error - 1; % Initializing Error 6 tol-1

OUT PUT SCREEN SHOT:-

I.lı Result

3 roots by Bisection Method: -3.000 .000 4.000 f 3 roots by Newtons Method -3.000 1.000 4.000 1.000 4.000>>

5 4 3 2 1 0 2 3 5 50 0 5 00 50 1 3

MAT LAB CODE:-

f = @(x) 2*x.^3 - 4*x.^2 - 22*x + 24;
df = @(x) 6*x.^2 - 8*x - 22;
x = linspace(-5,5);

% Plotting Function and zero line to see 3 roots
plot(x,f(x))
xlabel("x"); ylabel("f(x)");
grid on
yline(0); % plotting zero line to identify roots

%%%%%%%%% Bisection Method %%%%%%%%%%%
% For interval (-4,-2)
x1_bis = Bisection(f,-4,-2);
% For Interval (0,2)
x2_bis = Bisection(f,0,2);
% For Interval (3,5)
x3_bis = Bisection(f,3,5);

%%%%%%%%% Newton's Method %%%%%%%%%%%
% For starting value (-4)
x1_NR = NR(f,df,-4);
% For starting value (0)
x2_NR = NR(f,df,0);
% For starting value (3)
x3_NR = NR(f,df,3);

fprintf("\n 3 roots by Bisection Method : %.3f \t %.3f \t %.3f",x1_bis,x2_bis,x3_bis);
fprintf("\n 3 roots by Newton's Method : %.3f \t %.3f \t %.3f",x1_NR,x2_NR,x3_NR);


%Function for bisection method
function xr = Bisection(f,xl,xu)
% f is function
% xl ,xu are initial Guesses
error = 1; % Initializing Error
tol = 10^-6;
while(error>=tol)
xr = (xl+xu)/2; % New Guess Calculation
if f(xl)*f(xr) < 0 % Checking Condition for guess replacement
xu = xr;
else
xl = xr;
end
error = abs(f(xr)); % Defining error
end
end

% Function For NR
function x2 = NR(f,df,x1)
% f is function
% df is derivative function
% x1: Initial Guess
error = 1; % Initializing Error
tol = 1e-6; % Setting Tolerance
while (error >= tol)
x2 = x1 - (f(x1)/df(x1)); % Calculation of next guess
error = abs(f(x2)); % Defining Error
x1 = x2; % Replacing Guesses
end
end

Add a comment
Know the answer?
Add Answer to:
3. Write a code to find 3 roots of the function f(x)-2x3-4x2-22x+24 for the interval -5, 5] consi...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT