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

1 次查看(过去 30 天)
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 个)

David Jacob
David Jacob 2021-3-8
What is the error message? What is the value for n?
  3 个评论
David Jacob
David Jacob 2021-3-8
Have you run the function properly by defining sigma first and then calling the function with sigma as an input?
Walter Roberson
Walter Roberson 2021-3-9
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by