I am trying to edit an old code to simplify it but I keep getting errors. Right now I am getting the script name error even though I have followed instructions fo rnaming

13 visualizaciones (últimos 30 días)
I get the error saying i can't have the same script name as the function but it worked before and I haven't changed the name
%% problem 1c - projectile Quadratic
function [root] = Quadratic(a, b, c, plusOrMinus)
% Quadratic - determine the root of the projectile using the quadratic equation
% a, b, and c are the coefficients of the quadratic equation, plusOrMinus
% is a multiplier (+1 or -1) that selects the + or - in the quadratic
% formula. root is a single root of the quadratic equation that is
% computed using the quadratic formula.
% Erin Gibbs, u1556567, ME EN 1010, HW#2
root = (-b + plusOrMinus * (sqrt(b^2 - (4*a*c)))) / (2 * a);
end
%% test case 1 (positive plusOrMinus)
% equation: x^2 - x - 2 = 0
a = 1;
b = -1;
c = -2;
plusOrMinus = 1;
[root] = Quadratic(a, b, c, plusOrMinus)

Respuestas (1)

Star Strider
Star Strider el 26 de En. de 2025
Your code works here. I’m not quite sure what you’re doing.
You cannot have a script name with the same name as the function, however saving the function as ‘Quadratic.m’ should work.
%% problem 1c - projectile Quadratic
function [root] = Quadratic(a, b, c, plusOrMinus)
% Quadratic - determine the root of the projectile using the quadratic equation
% a, b, and c are the coefficients of the quadratic equation, plusOrMinus
% is a multiplier (+1 or -1) that selects the + or - in the quadratic
% formula. root is a single root of the quadratic equation that is
% computed using the quadratic formula.
% Erin Gibbs, u1556567, ME EN 1010, HW#2
root = (-b + plusOrMinus * (sqrt(b^2 - (4*a*c)))) / (2 * a);
end
%% test case 1 (positive plusOrMinus)
% equation: x^2 - x - 2 = 0
a = 1;
b = -1;
c = -2;
plusOrMinus = 1;
[root] = Quadratic(a, b, c, plusOrMinus)
root = 2
.
  4 comentarios

Iniciar sesión para comentar.

Categorías

Más información sobre Quadratic Programming and Cone Programming en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by