matlab program quadratic roots

3
Natacha Eguía, Sofía Jiménez, María Paz Delgado y la sueca Alexandra Larsson. Nina Francisca Nina Schreduders Natacha Eguía, Nina Francisca Nina Schreduders , Sofía Jiménez, María Paz Delgado y la sueca Alexandra Larsson. Naturally, we have to deliver two x-values. 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: 2x 2 + x - 1 = 0 We can call our function (first code) like this: x = rqe(2,1,-1) and we get from Matlab:

Upload: isidoro-vaquila

Post on 26-Aug-2014

112 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Matlab Program Quadratic Roots

Natacha Eguía, Sofía Jiménez, María Paz Delgado y la sueca Alexandra Larsson. Nina Francisca Nina Schreduders

Natacha Eguía, Nina Francisca Nina Schreduders , Sofía Jiménez, María Paz Delgado y la sueca Alexandra Larsson.

Naturally, we have to deliver two x-values.

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

Page 2: Matlab Program Quadratic Roots

n =     -1

http://www.matrixlab-examples.com/quadratic-equations.html

float a,b,c,x1,x2,disc;clrscr();printf("Enter the co-efficients\n");scanf("%f%f%f",&a,&b,&c);disc=b*b-4*a*c;/*to find discriminant*/if(disc>0)/*distinct roots*/{x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);printf("The roots are distinct\n");exit(0);}if(disc==0)/*Equal roots*/{x1=x2=-b/(2*a);printf("The roots are equal\n");printf("x1=%f\nx2=%f\n",x1,x2);exit(0);}x1=-b/(2*a);/*complex roots*/x2=sqrt(fabs(disc))/(2*a);printf("The roots are complex\n");printf("The first root=%f+i%f\n",x1,x2);printf("The second root=%f-i%f\n",x1,x2);getch();}