필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
Samuel Walker
Samuel Walker 2021년 3월 8일
댓글: Walter Roberson 2021년 3월 9일
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
Stephen23
Stephen23 2021년 3월 9일
Please show us exactly how you are calling q3_test1

댓글을 달려면 로그인하십시오.

답변 (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에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by