matlab simulink error help me

1 view (last 30 days)
Sutae
Sutae on 1 Dec 2012
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

Answers (2)

Mitch Martelli
Mitch Martelli on 2 Dec 2012
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 Comments
Mitch Martelli
Mitch Martelli on 3 Dec 2012
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
Mitch Martelli on 3 Dec 2012
M=rand(1,18100);
with for : Elapsed time is 4.856499 seconds.
without for : Elapsed time is 0.037057 seconds.

Sign in to comment.


Sutae
Sutae on 2 Dec 2012
Thanks, but I was having gave an error occurs
This error is Undefined function or variable 'cl'. The first assignment to a local variable determines its class.
and cl &k cd0l else
In my opinion, (i) think using those things tell me if there is another way
Note that I will try to run this program(simulink in matlab use matlab function)
  1 Comment
Walter Roberson
Walter Roberson on 2 Dec 2012
Pre-allocate "c1" and "cd01" and "k" and "cd" and "d"
Also double-check your complete routine for the case where M is empty.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!