I am calling outside functions for this code, but when I am getting an error saying I have too many output arguments. I think the error is within my function LandingDistance

%% problem 2
% user-defined function to compute the horizontal distance
% traveled by a projectile knowing the launch angle, the initial velocity,
% and geometric parameters defining the initial coordinates of the projectile.
d1 = 0.0876;
d2 = 0.1190;
v0 = 3.2;
thetaL = 50;
g = 9.81; % gravitational acceleration in m/s^2
% call initial coords function and initial velocity function
[x0, y0] = InitialCoords(d1, d2, thetaL); % input units: m, m, degrees, output units: m
[v0x, v0y] = InitialVelocityComponents(v0, thetaL); % input units: m/s de,grees, output units: m/s
%Solve the y equation for tLand by calling your Quadratic function.
a = -0.5*g; % g = acceleration due to gravity in m/s^2
b = v0y; % initial velocity in y direction
c = y0; % initial y position
plusOrMinus = -1;
root = Quadratic(a, b, c, plusOrMinus); % neg root
tLand = root;
% plug tLand into the x eqn to get xLand
xLand = LandingDistance(d1, d2, v0, thetaL);
fprintf('For launch angle = %.1f degrees and v0 = %.1f m/s, the landing distance is %.2f m \n', thetaL, v0, xLand)
Error using LandingDistance
Too many output arguments.
Error in HW3_projectile (line 36)
xLand = LandingDistance(d1, d2, v0, thetaL);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_____________________________________
function code
% landing distance function
function LandingDistance(d1, d2, v0, thetaL)
% x_Land --> the horizontal distance traveled by the projectile
g = 9.81; % gravitational acceleration in m/s^2
[x0, y0] = InitialCoords(d1, d2, thetaL) % input units: m, m, degrees, output units: m
[v0x, v0y] = InitialVelocityComponents(v0, thetaL) % input units: m/s de,grees, output units: m/s
a = -0.5*g; % g = acceleration due to gravity in m/s^2
b = v0y; % initial velocity in y direction
c = y0; % initial y position
plusOrMinus = -1;
[root] = Quadratic(a, b, c, plusOrMinus); % neg root
tLand = root
xLand = x0 + (v0x*tLand)
end
Not enough input arguments.
Error in LandingDistance (line 6)
[x0, y0] = InitialCoords(d1, d2, thetaL)
^^
>>

Answers (1)

The reason is that ‘LandingDistance’ doesn’t return any outputs:
function LandingDistance(d1, d2, v0, thetaL)
You probably want it to be:
function xLand = LandingDistance(d1, d2, v0, thetaL)
If so, that should work.
.
EDIT — Corrected typographical errors.

5 Comments

you're right, I realized that shortly after i posted the question. But now I am getting a different error. I also realized I had some equations in there wrong. I'm not sure if it's my quadratic function now, cause it's about the variable a, I've tried assigning it inside the landingDistance function and outside but it's not working.
% quadratic function
function [root] = Quadratic(a, b, c, plusOrMinus)
root = (-b + plusOrMinus * (sqrt(b^2 - (4*a*c)))) / (2 * a);
if (b^2 - (4*a*c)) >= 0
% Compute the root if the discriminant is non-negative
root = (-b + plusOrMinus * (sqrt(b^2 - (4*a*c)))) / (2 * a);
else
% Display error message for complex roots
error('The coefficients yield a complex root. Abort Program')
end
% landing distance function
function [xLand] = LandingDistance(d1, d2, v0, thetaL)
% xLand --> the horizontal distance traveled by the projectile
v0x = v0 * cosd(thetaL);
v0y = v0 * sind(thetaL);
x0 = d2*cosd(thetaL);
y0 = d1 + d2*sind(thetaL);
[root] = Quadratic(a, b, c, plusOrMinus); % neg root
tLand = root;
xLand = x0 + (v0x*tLand);
end
__________
d1 = 0.0876;
d2 = 0.1190;
v0 = 3.2;
thetaL = 50;
a = -0.5*g; % g = acceleration due to gravity in m/s^2
b = v0y; % initial velocity in y direction
c = y0; % initial y position
plusOrMinus = -1;
[xLand] = LandingDistance(d1, d2, v0, thetaL);
fprintf('For launch angle = %.1f degrees and v0 = %.1f m/s, the landing distance is %.2f m \n', thetaL, v0, xLand)
Unrecognized function or variable 'a'.
Error in LandingDistance (line 9)
[root] = Quadratic(a, b, c, plusOrMinus); % neg root
^
Error in test1 (line 12)
[xLand] = LandingDistance(d1, d2, v0, thetaL);
I figured it out, I needed to put the a, b, c, variables before calling the root function
In your code, you defined ‘a’, ‘b’, and ‘c’ however you passed ‘d1’, ‘d2’, and ‘v0’ (and ‘thetaL’) to ‘LandingDistance’. I’m honestly a bit lost as to what you should be passiinng to it.
I was missing an equation that needed a, b, and c. basically I was leaving out an equation and had variables assigned after calling functions instead of before

Sign in to comment.

Categories

Products

Asked:

on 27 Jan 2025

Edited:

on 27 Jan 2025

Community Treasure Hunt

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

Start Hunting!