For the cubic equation , 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 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!
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);
For the cubic equation , where a, b, c and d are real input coefficients. Write...
[MATLAB Coding] A four-bar linkage system is shown above. The first link, a, is an input link (crank) of length 1. The second link, b, is a coupler link of length 2. The third link, c, is an output link of length 4. The forth link, d, is the fixed link (ground) of length 5. All lengths are provided in metres. Please answer the whole question in MATLAB coding. 4 A four-bar linkage system is shown above. The first link,...
i need the answer to be on a MatLab window 1. Consider the following equation, which represents the concentration (c, in mg/ml) of a drug in the bloodstream over time (t, in seconds). Assume we are interested in a concentration of c2 mg/ml C3te-0.4t A. Estimate the times at which the concentration is 2 mg/ml using a graphical method Be sure to show your plot(s). Hint: There are 2 real solutions B. Use MATLAB to apply the secant method (e.g....
MATLAB Write an m-file capable of performing numerical integration for the equation using the simpsons and trapezoidal functions: function [value, error] = simpsons(func, a, b, n, I) %retuns the value and error ret = 0; h = (b-a)/(n+1); %step size pts = a:h:b; % array of points for i=2:(n+3)/2 a = 2*i-3; b = 2*i-2; c = 2*i-1; ret = ret + (func(pts(a)) + 4*func(pts(b)) + func(pts(c))); end value = h*ret/3; error = abs(I - value)*100/I; %error between value and...
Java Programming Question 4 (10 points): Solutions for a quadratic equation ax(squared)+bx+c= 0. where a does not equal zero are as follows. r1=( −b+√b(squared)−4ac)/2a r2=(−b−√b(squared)−4ac)/2a if b(squared)−4ac <0, equation doesn’t have real roots. If it is 0 there is one root(r1=r2). Write a Java program to read a,b and c from keyboard and find the roots, if they exist. Note: You need to have a method that takes 3 real values as arguments
in matlab -Consider the equation f(x) = x-2-sin x = 0 on the interval x E [0.1,4 π] Use a plot to approximately locate the roots of f. To which roots do the fol- owing initial guesses converge when using Function 4.3.1? Is the root obtained the one that is closest to that guess? )xo = 1.5, (b) x0 = 2, (c) x.-3.2, (d) xo = 4, (e) xo = 5, (f) xo = 27. Function 4.3.1 (newton) Newton's method...
1 Suppose V is the plane in R3 that contains the ponts a, b and g, where a (1,2,3), b-(1,-2,1) and c-(0,1,2) Suppose L is the lne in R3 that contains the pont a and is perpendicular to the plane V (a) Fınd an equation for V (b) Find an equation for L 2 Consıder the R2 - R function f defined by f (,y) (0,0) 2 (a) Write down the largest possıble doman Df of f (b) Prove from...
The following procedure can be used to determine the roots of a cubic equation a_3x^3 + a_2x^2 + a_1x + a_0 = 0: Set: A =a_2/a_3, B = a_1/a_3, and C = a_0/a_3 Calculate: D = Q^3 + R^2 where Q = (3B - A^2)/9 and R = (9AB - 27C - 2A^3)/54. If D > 0, the equation has complex roots. It D = 0, all roots are real and at least two are equal. The roots are given...
C++ The roots of the quadratic equation ax² + bx + c = 0, a ≠ 0 are given by the following formula: In this formula, the term b² - 4ac is called the discriminant. If b² - 4ac = 0, then the equation has a single (repeated) root. If b² - 4ac > 0, the equation has two real roots. If b² - 4ac < 0, the equation has two complex roots. Instructions Write a program that prompts the...
Suppose that 0 < a < b < c < d are Real numbers (they are all positive and from smallest to greatest in alphabetical order). Order the following fractions from SMALLEST to LARGEST: Suppose that 0 <a<b<c<d are Real numbers (they are all positive and from smallest to greatest in alphabetical order). Order the following fractions from SMALLEST to LARGEST: daba cbbc Choose the correct ordering from options below: O A. da a b C'b'c'b OB. a a d...