Dear Readers: Welcome to The Engineering Projects

## Wednesday, 30 January 2013

### Finding Roots & minimum Value of Quadratic Equations in MATLAB

Page Views:

Hello friends, hope you all are fine and enjoying good health. I am supposed to post the next topic of database tutorial in Microsoft Visual Studio 2010 but actually i couldn't get time to make that, so I thought of sharing this semester project with you guys. I will hopefully post the next part of that tutorial tomorrow.

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.

#### Functioning of Project

• This project takes three inputs from user.
• 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.
That's all for today, will meet you guys in the next lecture. Till then take care.
ALLAH HAFIZ .... :))

If you don't want to get yourself into Serious Technical Trouble while doing your programming OR technical projects then just sit back and relax and let us do the Job for you at a fairly reasonable cost. Submit your project details by Clicking Here »

I am Syed Zain Nasir, the founder of The Engineering Projects (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry.

 Follow @theenggprojects In 61 people's circles

# Subscribe To Get FREE Tutorials!

#### Confused? Feel free to ask

Your feedback is always appreciated. I will try to reply to your queries as soon as time allows.
Note:-
Please do not spam Spam comments will be deleted immediately upon my review.

Regards,