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

1 view (last 30 days)
Samuel Walker
Samuel Walker on 8 Mar 2021
Commented: Walter Roberson on 9 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

Answers (1)

David Jacob
David Jacob on 8 Mar 2021
What is the error message? What is the value for n?
  3 Comments
Walter Roberson
Walter Roberson on 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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by