This is a Numerical Analysis programSolutionSolution The mf
This is a Numerical Analysis program
Solution
Solution :
The m-file that we might use to accomplish this task is very simple:
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
OR
Solve the function you can
>> [x]=solve(\'a*x^2+b*x+c=0\',\'x\')
x =
-(b + (b^2 - 4*a*c)^(1/2))/(2*a)
-(b - (b^2 - 4*a*c)^(1/2))/(2*a)
For example:
>> [x]=solve(\'5*x^2+10*x+3=0\')
x =
- 10^(1/2)/5 - 1
10^(1/2)/5 - 1
>> [x]=solve(\'x^2-x-6=0\')
x =
-2
3

