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

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)

Risposte (1)

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 Commenti

it is saved as Quadratic.m in a folder named Quadratic but I still get the error
I’m slightly lost at this point.
Only the function code itself:
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
should be saved in ‘Quadratic.m’.
Then in your script (that is not named ‘Quadratic.m’ although it can be named ‘Erin_Gibbs_u1556567_ME_EN_1010_HW#2’ or something similar) call your funcittion as:
%% 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)
That should work.
.
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Accedi per commentare.

Prodotti

Richiesto:

il 26 Gen 2025

Commentato:

il 26 Gen 2025

Community Treasure Hunt

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

Start Hunting!

Translated by