not sure how to solve this issue with function arguments,
I'm trying to do this to figure out how to return a vector y that contains both solutions: a logical variable isreal indicating whether the results were real (isreal=1) or imaginary (isreal=0). not sure if it would be the right approach.
Any help will be appreciated, thank you
myquad
Not enough input arguments.
Error in myquad (line 3)
D = b^2 - 4*a*c;
^
function [y, isreal] = myquad(a, b, c)
% Calculate the discriminant
D = b^2 - 4*a*c;
% Calculate the two solutions using the quadratic formula
y(1) = (-b + sqrt(D)) / (2*a);
y(2) = (-b - sqrt(D)) / (2*a);
% Check if the solutions are real
isreal = all(isreal(y));
% If the discriminant is negative, the solutions are complex
if D < 0
y = [NaN, NaN]; % Return NaN for complex solutions
isreal = 0; % Set isreal to false
end
end
%In the command window I'm trying to call the function like this;
y = myquad(1, -2, 1);
disp(y); % Output: [1, 1]
disp(isreal); % Output: 1

1 comentario

VBBV
VBBV el 29 de Ag. de 2025
Editada: VBBV el 29 de Ag. de 2025
Please check the version of Matlab you are using . In some older version, the function defintions are not allowed before actually calling the function in the script file.

Iniciar sesión para comentar.

 Respuesta aceptada

Torsten
Torsten el 29 de Ag. de 2025
Editada: Torsten el 29 de Ag. de 2025
Don't use isreal as a variable name: isreal is a MATLAB function that you overwrite this way.
[y, is_real] = myquad(1, -2, 1);
disp(y); % Output: [1, 1]
1 1
disp(is_real); % Output: 1
1
function [y, is_real] = myquad(a, b, c)
D = b^2 - 4*a*c;
if D >=0
is_real = true;
y = [(-b + sqrt(D)) / (2*a),(-b - sqrt(D)) / (2*a)];
else
is_real = false;
y = [NaN, NaN];
end
end

4 comentarios

Cesar Cardenas
Cesar Cardenas el 29 de Ag. de 2025
Thank you, still getting the same error, sorry, not so good at matlab, can you show how to call the function?
Matt J
Matt J el 29 de Ag. de 2025
Editada: Matt J el 29 de Ag. de 2025
@Cesar Cardenas Torsten and I have both run our code for you so you can see how the function is called and that they produce the proper output. Please run your code for us online, so that we can all see it in action.
function [y, is_real] = myquad(a, b, c)
D = b^2 - 4*a*c;
if D >=0
is_real = true;
y = [(-b + sqrt(D)) / (2*a),(-b - sqrt(D)) / (2*a)];
else
is_real = false;
y = [NaN, NaN];
end
end
[y, is_real] = myquad(1, -2, 1);
disp(y); % Output: [1, 1]
disp(is_real); % Output: 1
Cesar Cardenas
Cesar Cardenas el 29 de Ag. de 2025
I found my mistake, it works now, thank you

Iniciar sesión para comentar.

Más respuestas (1)

Matt J
Matt J el 29 de Ag. de 2025
Editada: Matt J el 29 de Ag. de 2025
Your code uses 'isreal' as both the name of a function and a variable. You can resolve the conflict by renaming the output, e.g.,
[y,isReal] = myquad(1, -2, 1)
y = 1×2
1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
isReal = logical
1
function [y, isReal] = myquad(a, b, c)
% Calculate the discriminant
D = sqrt(b^2 - 4*a*c);
isReal=isreal(D);
if ~isReal, y=nan(1,2); return; end
% Calculate the two solutions using the quadratic formula
y = (-b + [-1,1]*D) / (2*a);
end

Categorías

Etiquetas

Preguntada:

el 29 de Ag. de 2025

Editada:

el 29 de Ag. de 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by