Question

LAB SECTION: NAME: PERSON C Hw5 Problem 3 (18 points) The following procedure can be used to determine the roots of a cubic equation a3x3 2x2 aux a Set: A and C Calculate: D Q3 R Where Q (3 A2)/9 and (9AB 27c 2A3)/54 If D 0, the equation has complex roots. If D 0, all roots are real and at least two are equal. The roots are given by: 2 WR A/3, and x2 WR A/3 If D 0, all roots are real and are given by: V-Q cos (0/3)- A/3, x 2 V-9 cos e/3 120o) A/3, and x3 2 V-Q cos(6/3 2400) A/3 where cos 0 R/V-Q3. Write a MATLAB program in a script file that determines the real roots of a cubic equation. As input, the program asks the user to enter the values of a3, a2, ai, and ao as a vector. The program then calculates the value of D. If the equation has complex roots the message The equation CCCx +CCC +CCC +CCC has complex roots is displayed. Otherwise the real roots are calculated and displayed as The roots of the equation CCCx 3 +CCCx 2 +CCCX+CCC are XXX, XXX, and XXX. Where CCC refer to the value of the corresponding coefficient, refer to the sign of the coefficient or and XXX refer to the value of the roots. Use fprintf to display the text and data. The data should be displayed in g format Use the program to determine the roots of the following equations (a) 5x3 34.5 x 2 36.9 x 8.8 o (b) 2x 10 x 2 24 x 15 0 (c) x3 9x2 24 x 20 0
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Matlab code to find the roots of cubic equation and the code has to be written in script file:-

The code is explained in the form of comments i.e followed by the symbol %

prompt='enter the values of a3,a2,a1,a0 respectively \n'; % to display the message to user
x=input(prompt); % input entered by the user will be stored in x in the form of vector
a3=x(1); % initializing a3
a2=x(2); % initializing a2
a1=x(3); % initializing a1
a0=x(4); % initializing a0
A=a2/a3; % assigning the value of A as given by the formula for A in the given problem
B=a1/a3; % assigning the value of B as given by the formula for B in the given problem
C=a0/a3; % assigning the value of C as given by the formula for C in the given problem
Q=(3*B-A*A)/9; % assigning the value of Q as given by the formula for Q in the given problem
R=(9*A*B-27*C-2*A*A*A)/54; % assigning the value of R as given by the formula for R in the given problem
D=(Q*Q*Q)+(R*R); % assigning the value of D as given by the formula for D in the given problem
formatspec='The equation %4.2fx^3+%4.2fx^2+%4.2fx+%4.2f has complex roots \n';
% formatspecifier to set the format for complex roots
formatspec1='The roots of the equation %4.2fx^3 + %4.2fx^2 + %4.2fx + %4.2f are %4.2f , %4.2f and %4.2f \n';
% formatspecifier to set the format for other than complex roots
if D>0 % for D>0
fprintf(formatspec,x); % print that there are complex roots
end
if D==0 % for D==0
x1=(2*R^(1/3))-(A/3); % caluculate x1
x2= -(R^(1/3))-(A/3); % caluculate x2
x3=x2; % x2 and x3 are equal
fprintf(formatspec1,a3,a2,a1,a0,x1,x2,x3); % print the roots
end
if D<0
t=acosd(R/((-Q)^(3/2))); % caluculate theta
x1=2*((-Q)^(1/2))*(cosd(t/3))-(A/3); % caluculate x1
x2=2*((-Q)^(1/2))*(cosd(t/3+120))-(A/3); % caluculate x2
x3=2*((-Q)^(1/2))*(cosd(t/3+240))-(A/3); % caluculate x3
fprintf(formatspec1,a3,a2,a1,a0,x1,x2,x3); % print the roots
end
  
Output:-

After writing the above code run the program and enter the values for a3,a2,a1 and a0 in the command prompt for the given respective 3 equations to get the following output.

Command Window >> roots enter the values of a3,a2,al, ao respectively [5 -34.5 36.9 8.8 The roots of the equation 5.00x*3-34.

Add a comment
Know the answer?
Add Answer to:
The following procedure can be used to determine the roots of a cubic equation a_3x^3 +...
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