Question

Using matlab and if/else statement please!

Write a function that determines the real roots of a quadratic equation ax2 + bx + c = 0. To calculate the roots of the equation, the function calculates the discriminant D, given by: D = b2-4ac If D> 0, the code should display The equation has two roots and print the values on a new line. If D 0, the code should display The equation has one root., and print the value on a new line. If D <0, the code should display The equation has no real roots. The function must take three inputs, a, b, and c (in that order), and provide two outputs (the two roots), with NaN as the default value if less than two roots exist. For example if there is one real root of 4, the function returns 4 and NaN. If there are no real roots, it returns NaN and NaN. f there are two real roots, the function must return them in ascending order. For example, if the roots are 4 and 2, it must return them in the order: 2, 4. As with the previous problem, it is up to your discretion whether the code to display the root(s) is included in the function or in the script. In the script, run the following test cases: a. 2x2+8x+8 0 b. -5x2 3x-4 0 c. -2x2+ 7x +4 -0

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

%%%%%%%%%%%%%save this as cal_root.m

function [root1,root2]=cal_root(a,b,c)
d=b^2-4*a*c;
if(d>0)
root1=(-b-d^0.5)/(2*a);
root2=(-b+d^0.5)/(2*a);
if(root1>root2)
temp=root1;
root1=root2;
root2=temp;
end
elseif(d==0)
root1=-b/(2*a);
root2=NaN;
else
root1=NaN;
root2=NaN;
end
end

%%%%%%%%%%%%%%%end of file1

%%%%%start of file 2

[root1,root2]=cal_root(2,8,8);
fprintf('roots for 2x^2+8x+8=0\n');
disp(root1)
disp(root2)
[root1,root2]=cal_root(-5,3,-4);
fprintf('roots for -5x^2+3x-4=0\n');
disp(root1)
disp(root2)
[root1,root2]=cal_root(-2,7,4);
fprintf('roots for -2x^2+7x+4=0\n');
disp(root1)
disp(root2)

MATLAB R2017a HOME Search Documentation Log In ▼ Go To▼ Comment ge-ese Breakpoints Run Run and ES Compare New Open Save Advance Run and Indent E Print Find ▼ Advance Current Folder Editor - GAMATLABR2017a bin Untitled57.m Name ▲ 田山m3iregistry A [rooti,root2]-cal_root [2, 8,8) registry util 2- fprintf( roots for 2x^2+8x+8=0\n) ; 3disp (root1) 4 -d1sp (root2) 5 -[rooti, root2]-cal root (-5, 3,-4); 6- fprintf( roots for -5x^2+3x-4=0\n ); 7-disp (rooti) Bdisp (root2) 9[rooti, root2]-cal_root (-2,7,4): 田」 win64 cal root.asv cal root.m fractalTreeBasic.m fractalTriangle.m imageProcessing.m Command Window >Untitled57 IterMeth.m Icdata.ml Details Works Name- NaN root3 for -5x^2+3x-4=0 Value NaN 0.2500 1,0000e-06 9.8000 13 68.1000 0.5000 NaN -0.5000 rooti 20 2 script Ln 10 Col 34

Add a comment
Know the answer?
Add Answer to:
Using matlab and if/else statement please! Write a function that determines the real roots of a...
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
  • 4-6 on matlab 4. Write a program in a script file that determines the real roots...

    4-6 on matlab 4. Write a program in a script file that determines the real roots of a quadratic equation ax2+bx+c 0 When the file runs, it asks the user to enter the values of the constants a, b, and c. To calculate the roots of the equation the program calculates the discriminant D, given by: D b2-4ac When D 0, the program displays message "The equation has two roots," and the roots are displayed in the next line. When...

  • write a C programming code for the following prompt. please use stucture concept and test if...

    write a C programming code for the following prompt. please use stucture concept and test if the code works perfectly. Project Description: In this project, you will write a program to calculate the roots of a quadratic equation. Structure concepts will be used in this project. Your program will prompt the user to enter the coefficients of a quadra coefficientsType. Then it will compute the roots of the quadratic equation and store the result in a structure variable of type...

  • Rule book: 1.)Must be Python - 2.) no imported module (ie cmath) - no can do...

    Rule book: 1.)Must be Python - 2.) no imported module (ie cmath) - no can do on the imports... pure code.. 3.) numbers are float - not integer. Here's the prompt: (Algebra: solve quadratic equations) The two roots of a quadratic equation ax^2 + bx + c = 0 can be obtained using the following formula: r1 = (-b + sqrt(b^2 - 4ac)) / (2a) and r2 = (-b - sqrt(b^2 - 4ac)) / (2a) b^2 - 4ac is called...

  • #6 Write a Matlab program that finds numerically all the roots (or the zeros) of the...

    #6 Write a Matlab program that finds numerically all the roots (or the zeros) of the algebraic equation below in the interval 1.0 <=x<=3.0: sqrt(log(x^2+1))=x^2*sin(e^x)+2 Part a) Prompt the user to enter a positive integer number n, defined the range 2<=n<=15, and then verify if the number entered lies within the specifications. Your program must allow the user to reenter the number without the need to rerun the program. Part b) Create a user-defined function for the bisection method(see details...

  • You are given the following function () 6a +11 6.1 You are to find the roots of this function using the secant method with z 2.5-and zi = 3.5 1.(6 points) Develop a function m-file that calculate...

    You are given the following function () 6a +11 6.1 You are to find the roots of this function using the secant method with z 2.5-and zi = 3.5 1.(6 points) Develop a function m-file that calculates the root of the above function using the secant method. The function should have the following five inputs only. 1. (2 points) The equation or function whose roots need to be found 2.(2 points) Initial guess -1 3. (2 points) Initial guess A4.(2...

  • A quadratic equation is generally represented as, ax^2 + bx + c The root(s) of the...

    A quadratic equation is generally represented as, ax^2 + bx + c The root(s) of the above quadratic equation is computed using the following formula, root1 = (-b + sqrt(D))/ 2a root2 = (-b - sqrt(D))/2a Where D is the discriminant of the quadratic equation and is computed as, D = b^2 - 4ac Given the value of D, the roots of a quadratic equation can be categorized as follows, D > 0 : Two distinct real roots D =...

  • Need help with number 3, thanks! isplays l itnuml is less than num2. 2. (50+30- 80...

    Need help with number 3, thanks! isplays l itnuml is less than num2. 2. (50+30- 80 Points) Write a MATLAB function numcomp502 (numl, num2) that takes two numbers numl and as input and returns num3 as output, such that num3 is equal to 0, 1, and-1 depending on whether numl is equal to, greater than or less than num2 respectively. Test your code with the pairs (10, 5), (-3,-3), and (2, 10) as input. Attach a screen-shot. 3. 80 Points...

  • *MATLAB CODE* 3)Write a function 'mydsort' that sorts a vector in descending order (using a loop,...

    *MATLAB CODE* 3)Write a function 'mydsort' that sorts a vector in descending order (using a loop, not the built-in sort function). 4)write a function that will receive a vector and will return two index vectors: one for ascending order and one for descending order. Check the function by writing a script that will call the function and then use the index vectors to print the original vector in ascending and descending order. **Please make sure they work. I have found...

  • 3. Produce the function quadratic(a,b,c) where a, b, and c are the coeffi- cients of the...

    3. Produce the function quadratic(a,b,c) where a, b, and c are the coeffi- cients of the quadratic ax2 + bx +c. The function should return: 1. a list of the two roots of ax2 + bx +c= 0, when two roots exist 2. the first root is the smaller of the two 3. if ax2 + bx +c=0 has no real roots, then return None. 4. if the quadratic has one real root only, then return a list with a...

  • MATLAB Code: ?MATLAB Code: Write a function that converts a given letter grade it will print...

    MATLAB Code: ?MATLAB Code: Write a function that converts a given letter grade it will print its equivalence in numerical scale. For example if the user enters A, then your function should print a message stating that the grade must be between 90 and 100. B is between 80 and 89, etc. Everything under 60 is F. Use the switch-case function. Use fprintf to display the results on the screen.

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