0% found this document useful (0 votes)
20 views

Matla Assignment 3

The document describes MATLAB code for solving quadratic equations. It provides two functions, rqe and rqe2, that calculate the roots of a quadratic equation given by coefficients a, b, and c. Rqe returns the two roots in a vector while rqe2 returns them in separate variables. An example call of each function is given to solve the equation 2x^2 + x - 1 = 0, returning roots of 0.5 and -1.

Uploaded by

Nasrul Torres
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Matla Assignment 3

The document describes MATLAB code for solving quadratic equations. It provides two functions, rqe and rqe2, that calculate the roots of a quadratic equation given by coefficients a, b, and c. Rqe returns the two roots in a vector while rqe2 returns them in separate variables. An example call of each function is given to solve the equation 2x^2 + x - 1 = 0, returning roots of 0.5 and -1.

Uploaded by

Nasrul Torres
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

ASSIGNMENT 3 MATLAB

1)
The command that we used to complete the task that been given during the class sesssion is
as shows below:
function x = rqe(a,b,c)
x(1) = (-b + sqrt(b^2 - 4 * a * c))/(2*a);
x(2) = (-b - sqrt(b^2 - 4 * a * c))/(2*a);

We enter the coefficients as parameters when we call the function. We assign to variables x(1)
and x(2) the calculated values using the formula, and the returning result x is a vector
containing x(1) and x(2).
We could use the following Matlab code instead, to return two separate variables:
function [x,y] = rqe2(a,b,c)
x = (-b + sqrt(b^2 - 4 * a * c))/(2*a);
y = (-b - sqrt(b^2 - 4 * a * c))/(2*a);

If we want to compute the roots of the following expression:


2x2 + x - 1 = 0

We can call our function (first code) like this:


x = rqe(2, 1, -1)

and we get from Matlab:


x=

0.5000

-1.0000

We can call our second function (second code above) like this:
[m, n] = rqe2(2, 1, -1)

and we get from Matlab:


m =

0.5000

n =

-1

2)

You might also like