Hello friends, hope you all are fine and enjoying good health. In today's tutorial, I am going to share How to find Roots of Quadratic Equations in MATLAB. It's quite a quick tutorial in which I will give you the code and will explain it a little. Benefit of this project is that when you are learning MATLAB then you must design such small codes so that you know more about the behavior of MATLAB.
Today two of my juniors came to me for a simple MATLAB term project. It's quite an easy project but i thought to share it for those students who are dealing with basics of MATLAB. Mostly such projects are offered to students in first or second semester when they have very basic knowledge of MATLAB coding and they feel helpless while solving such problems as is the case with those two students. So, let's get started with How to find Roots of Quadratic Equations in MATLAB.
Roots of Quadratic Equations in MATLAB
- This finding Roots of Quadratic Equations in MATLAB takes three inputs from user.
- Variables of Quadratic Equation.
- Domain limit.
- Variable to choose whether to show roots of the equation or minimum of function or both.
- After taking these inputs the function calculates the roots of the quadratic equations.
- After that finds the minimum of the function within the domain limit.
- And finally show both of them on the graph.
Code of the Project
MATLAB is a really amazing tool and I really love to do coding in it.The code for this project is given below. I didn't give description of the code because its a simple project and second thing I want the young students to search these commands themselves so that they could learn something. Second thing MATLAB has a very strong help section, if you are having problem with any command simply enter doc [Command] or help [Command] in the main window and MATLAB will give you everything you need related to that command. Here's the code :%==== Code Starts Here(www.TheEngineeringProjects.com) ==== clc clf % ============ Taking Inputs From User ============== handle = input('Enter the handle of the function : '); limit = input('Enter the domain limits : '); initial = input('Enter the initial solution estimate : '); k = input('Enter 1 for min, 2 for roots & 3 for both : '); syms x; a1 = 100000; %====== Calculating Roots of the Quadratic Equation ========= func = @(x)handle(1,1)*x^2 + handle(1,2)*x + handle(1,3); root1 = (-handle(1,2) + sqrt((handle(1,2)^2)-(4*handle(1,1)*handle(1,3))))/(2*handle(1,1)); root2 = (-handle(1,2) - sqrt((handle(1,2)^2)-(4*handle(1,1)*handle(1,3))))/(2*handle(1,1)); roots=[root1,root2]; %====== Calculating Minimum Value Within Domain Limits ========= for x = limit(1,1):0.1:limit(1,2) a = func(x); if (a < a1) a1 = a; x1 = x; end if(k==1 || k==3) plot(x,a,'--rs','LineWidth',2,... 'MarkerEdgeColor','k',... 'MarkerFaceColor','g',... 'MarkerSize',10) hold on; end end min = a1; %====== Displaying Roots & Minimum Values ========= if ( k == 1) min end if ( k == 2) roots plot(x,root1,'--rs','LineWidth',2,... 'MarkerEdgeColor','k',... 'MarkerFaceColor','r',... 'MarkerSize',10) hold on; plot(x,root2,'--rs','LineWidth',2,... 'MarkerEdgeColor','k',... 'MarkerFaceColor','r',... 'MarkerSize',10) end if ( k == 3) min roots plot(x,root1,'--rs','LineWidth',2,... 'MarkerEdgeColor','k',... 'MarkerFaceColor','r',... 'MarkerSize',10) hold on; plot(x,root2,'--rs','LineWidth',2,... 'MarkerEdgeColor','k',... 'MarkerFaceColor','r',... 'MarkerSize',10) end %==== Code Ends Here(www.TheEngineeringProjects.com) =======
Test Input
- For the testing purposes, use the below values as a testing input :
- First input = [1 5 6]
- Second input = [-1 1]
- Third input = 0
- Fourth input = 3
- The Code is self explanatory but if anyone having any problem in it may ask in comments.