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 vues (au cours des 30 derniers jours)
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)

Réponses (1)

Star Strider
Star Strider le 26 Jan 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 commentaires
Star Strider
Star Strider le 26 Jan 2025
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Quadratic Programming and Cone Programming dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by