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
Mostra commenti meno recenti
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)
.
4 Commenti
ERIN
il 26 Gen 2025
Spostato: Star Strider
il 26 Gen 2025
Star Strider
il 26 Gen 2025
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.
.
ERIN
il 26 Gen 2025
Spostato: Star Strider
il 26 Gen 2025
Star Strider
il 26 Gen 2025
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
Categorie
Scopri di più su State-Space Control Design in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!