I can't figure out why n=length(sigma) has gone wrong

1 vue (au cours des 30 derniers jours)
Samuel Walker
Samuel Walker le 8 Mar 2021
function [V1,V2]=q3_test1(K,CD0,S,sigma,W,T0)
n = length(sigma);
V1=zeros(1,n);
V2=zeros(1,n);
for i = 1:n
T = T0*sigma(i);
rho = 1.225*sigma(i);
if T^2 < 4*K*CD0*W^2
V1 = -1;
V2 = -1;
else
X1 = (T-sqrt(T^2-4*K*CD0*W^2))/2*CD0;
V1 = sqrt(X1/(0.5*rho*S));
X2 = (T+sqrt(T^2-4*K*CD0*W^2))/2*CD0;
V2 = sqrt(X2/(0.5*rho*S));
end
end
end
  1 commentaire
Stephen23
Stephen23 le 9 Mar 2021
Please show us exactly how you are calling q3_test1

Connectez-vous pour commenter.

Réponses (1)

David Jacob
David Jacob le 8 Mar 2021
What is the error message? What is the value for n?
  3 commentaires
David Jacob
David Jacob le 8 Mar 2021
Have you run the function properly by defining sigma first and then calling the function with sigma as an input?
Walter Roberson
Walter Roberson le 9 Mar 2021
You cannot just run the code by pressing the green Run button.
Well, you can do that, but the result is the error message you see,
When you run a function, MATLAB will never look in the calling environment to see if a variable has been defined with the same name as a parameter that has not been passed in. So when you run the code by pressing the green Run button, MATLAB will not check the base workspace (or any other workspace) to see if it can find values for K, CD0, S, sigma, W, or T0 to use in the function.
There are some circumstances under which other variables might be looked for in the calling environment, but never for variables named in the list on the function statement.

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by