solve quadratic function with two input and three output in matlab

To solve a quadratic function with two inputs and three outputs in MATLAB, you can use the roots function. The roots function returns the roots of a polynomial equation.

Here's an example of how you can solve a quadratic function with two inputs (a and b representing the coefficients of the equation) and three outputs (the solutions):

main.m
function [x1, x2, x3] = solveQuadratic(a, b)
    c = 0;  % coefficient of x^2 term
    equation = [a b c];  % create the quadratic equation
    
    % Use the roots function to solve the quadratic equation
    rootsArray = roots(equation);
    
    % Separate the roots into three output variables
    x1 = rootsArray(1);
    x2 = rootsArray(2);
    x3 = rootsArray(3);
end
374 chars
13 lines

You can use this function by passing in the a and b coefficients of the quadratic equation and the function will return the three solutions as output.

For example:

main.m
[a, b] = solveQuadratic(1, -3);  % calling the function with a = 1 and b = -3
disp(a);
disp(b);
disp(c);
105 chars
5 lines

This will output the three solutions 2, 1, and 0.

gistlibby LogSnag