Question

For the cubic equation , where a, b, c and d are real input coefficients. Write...

For the cubic equation a^{3}x+b^{2}x+cx+d=0 , where a, b, c and d are real input coefficients. Write a MATLAB function root.m of the form:

function [largestRoot] = root(a, b, c, d)

% a: Coefficient of x^3

% b: Coefficient of x^2

% c: Coefficient of x

% d: Coefficient of 1

% largestRoot: The largest real root of the cubic

to find the largest real root of this equation accurate to within a relative error 10^{-6} using any methods such as Newton's, Horner's.... Any of your choice. And should not use the MATLAB functions fzero, roots or eig.

I got some root comparison code:

%%%%%
function [a, b, c, d] = rootsToCoeffs(r1, r2, r3)
    a = 1;
    b = -(r1 + r2 + r3);
    c = r1 * r2 + r2 * r3 + r3 * r1;
    d = -(r1 * r2 * r3);
end
%%%%%%%%
function maxRelErr = compareRoots(expected, actual)
    maxRelErr = -Inf;
    if expected == 0
        if actual != 0
            % If you wanted 0 but didn't have it, infinite error.
            maxRelErr = Inf;
        end
    else
        relErr = abs(expected - actual) / abs(expected);
        % Make sure error of 0.0 isn't a bad thing.
        if relErr != 0
            maxRelErr = relErr;
        end
    end
end

Please write in a basic way if possible. I'm just a beginner to MATLAB. Thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer: function return f-val at x for given coefficients function kval-find?(a.biS.dix.) Afval end function to find thelarge

COPYABLE CODE:

%function return f-val at x for given coefficients

function fval=findF(a,b,c,d,x)

     %fval

     fval=a*(x.^3)+b*(x.^2)+c*x+d;

end

%function to find thelargest cubic roots

%bisection method is used to find the largest root

function [largestRoot] = root(a, b, c, d)

     %tolerance

     accur = 1e-6;

     %number of steps

     nSteps = 1e-5;

     %starting x value

     start = 1e-3;

     %ending x value

     ed = 1e3;

%loop untill we fin the largest root with said tolerance value

while (ed - start >= nSteps || ( abs( findF(a,b,c,d,start) ) >= accur && abs( findF(a,b,c,d,ed) ) >= accur ) )

          %middle of start and ed

          cval = (start + ed)/2;

          %find fVal at cval and check fval is 0.

          if ( findF(a,b,c,d,cval) == 0 )

              %If fval is 0, we found root.

              %so exit from loop

              break;

          %Check in what half does root falls

elseif ( findF(a,b,c,d,start)*findF(a,b,c,d,cval) < 0 )      

              ed = cval;

          else

              start = cval;

          end

     end

     %Get largest root

     if(a>b)

          largestRoot=start;

     else

          largestRoot=ed;

     end

%end function

end

%code to test function

%cubic function: f(x)=x^3-2

largestRoot=root(1,0,0,-2);

%display largest root

fprintf("LARGEST ROOT: %8.4f",largestRoot);

Add a comment
Know the answer?
Add Answer to:
For the cubic equation , where a, b, c and d are real input coefficients. Write...
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