matlab simulink error help me
이전 댓글 표시
Hi I'm korean student
Error occurs I use the matlab in simlunk
I'm not actually sure why this error till you do not know how to fix
please teach me and help me matlab masters
code is
M size is 1 X 181
W and S is constant
this code is matlab function's parameter in simulink
-----------------------------------------------------------------------------
function [D,cd]= fcn(S, W, M)
%#codegen
for i=1:181;
cl=W./(0.5*2116.2*1.4*S*M.^2);
if M(1,:)<=0.8 ;
cd0l=0.015;
k=1/(pi*6*0.7);
elseif M(1,:)>=1.2;
cd0l=(0.015+(3*0.015-0.015)*(sin((2*pi)/3))^3);
k=(1/(pi*6*(0.7-7*(0.4)^2+22*(0.4)^3)));
else
cd0l=(0.015+(3*0.015-0.015).*sin((M-0.8)./0.6*pi).^3);
k=(1./(pi*6*(0.7-7*(M-0.8).^2+(M-0.8).^3).*22));
end
cd=cd0l+k.*cl.^2*ones(1, 181);
D=0.5.*cd.*1.4.*2116.2.*S.*M.^2;
----------------------------------------------------------------------------
Where I want the value of the cd and D value. and Play the simulink and this error is
Size mismatch (size [:? x 1] ~= size [181 x 1]).
Mismatched varying and fixed sizes indicate a probable run-time error. If this diagnostic is incorrect, use indexing to explicitly make the varying size fixed
Please tell me what the error is gone how
답변 (2개)
Mitch Martelli
2012년 12월 2일
Hi, You use a for statement but I cant see no one indexed variable inside the loop. Maybe you would like to to this :
num_col=size(M,2);
for i=1:num_col;
cl(i)=W./(0.5*2116.2*1.4*S*M(i).^2);
if M(i)<=0.8 ;
cd0l(i)=0.015;
k(i)=1/(pi*6*0.7);
elseif M(i)>=1.2;
cd0l(i)=(0.015+(3*0.015-0.015)*(sin((2*pi)/3))^3);
k(i)=(1/(pi*6*(0.7-7*(0.4)^2+22*(0.4)^3)));
else
cd0l(i)=(0.015+(3*0.015-0.015).*sin((M(i)-0.8)./0.6*pi).^3);
k(i)=(1./(pi*6*(0.7-7*(M(i)-0.8).^2+(M(i)-0.8).^3).*22));
end
cd(i)=cd0l(i)+k(i).*cl(i).^2;
D(i)=0.5.*cd(i).*1.4.*2116.2.*S.*M(i).^2;
end
If yes, you can this program without use for statement in a easy form.
M1tCh
댓글 수: 2
Mitch Martelli
2012년 12월 3일
Without for loop :
cl_trial=W./(0.5*2116.2*1.4*S*M.^2);
cd0l_trial=(0.015+(3*0.015-0.015).*sin((M-0.8)./0.6*pi).^3);
k_trial=(1./(pi*6*(0.7-7*(M-0.8).^2+(M-0.8).^3).*22));
case_a=find(M<=0.8);
cd0l_trial(case_a)=0.015;
k_trial(case_a)=1/(pi*6*0.7);
case_b=find(M>=1.2);
cd0l_trial(case_b)=(0.015+(3*0.015-0.015)*(sin((2*pi)/3))^3);
k_trial(case_b)=(1/(pi*6*(0.7-7*(0.4)^2+22*(0.4)^3)));
cd_trial=cd0l+k.*cl_trial.^2;
D_trial=0.5.*cd.*1.4.*2116.2.*S.*M.^2;
Mitch Martelli
2012년 12월 3일
M=rand(1,18100);
with for : Elapsed time is 4.856499 seconds.
without for : Elapsed time is 0.037057 seconds.
Sutae
2012년 12월 2일
0 개 추천
댓글 수: 1
Walter Roberson
2012년 12월 2일
Pre-allocate "c1" and "cd01" and "k" and "cd" and "d"
Also double-check your complete routine for the case where M is empty.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!