Using a for loop to fill up a matrix

2 views (last 30 days)
Novice here, and I can't figure out how to fill up a matrix using the for loop. The program computes the correct values, but the output is really unprofessional looking. I should be able to get the three output matices: A matrix, CLa and CDia. After each pass in the loop, in the A matrix I should get a column of length four, and the CLa and CDia matrices yield one value each for each pass through the loop.
Here is my program: (I know it's ugly, but I'm new and it works!). The meat of the program is down where the n matrix goes through and uses each successive value of alfa. Right below that is where I want the matrices to start filling up.
for alfa = [-2:2:12],
format long e;
theta=[22.5 45 67.5 90];
k1=sin(theta*pi/180);
k2=sin(3*theta*pi/180);
k3=sin(5*theta*pi/180);
k4=sin(7*theta*pi/180);
k=[k1;k2;k3;k4];
mu=0.24933*(1-0.6*cos(theta*pi/180));
l1=mu+k1;
l2=3*mu+k1;
l3=5*mu+k1;
l4=7*mu+k1;
l=[l1;l2;l3;l4];
m1=k1.*l1;
m2=k2.*l2;
m3=k3.*l3;
m4=k4.*l4;
m=[m1;m2;m3;m4];
n=((alfa+1.2)*pi/180)*(mu.*k1);
A(:,:)=m'\n'
CLa(1,:)=A(1)*pi*9
CDia(1,:)=(CLa^2/(9*pi))*(1 + 3*A(2)^2/A(1)^2 + 5*A(3)^2/A(1)^2 + 7*A(4)^2/A(1)^2)
end;

Accepted Answer

Matt Fig
Matt Fig on 21 Apr 2011
Please go back and format your code using the '{} Code' button.
EDIT Thank you for (partially) formatting your code!
There are some questions having to do with what you want. First off, putting everything that doesn't change in the FOR loop is very bad form. I know you are a beginner, so I am just pointing that out for future reference :-). Now the question is, what is the line:
A(:,ii) = m'\n'
supposed to be doing? Are you meaning to do m\n' (matrix division?) or what? Here is your code, partially fixed, and assuming you did mean to use matrix division. Also, pay attention to the lines assigning CDia and Cla. Did you mean to access each of those rows in the current column of A? If so,r replace A(1) with A(1,ii), and similarly for A(n), etc...
% All of this stuff goes outside the loop!
format long e;
theta=[22.5 45 67.5 90];
k1=sin(theta*pi/180);
k2=sin(3*theta*pi/180);
k3=sin(5*theta*pi/180);
k4=sin(7*theta*pi/180);
k=[k1;k2;k3;k4];
mu=0.24933*(1-0.6*cos(theta*pi/180));
l1=mu+k1;
l2=3*mu+k1;
l3=5*mu+k1;
l4=7*mu+k1;
l=[l1;l2;l3;l4];
m1=k1.*l1;
m2=k2.*l2;
m3=k3.*l3;
m4=k4.*l4;
m=[m1;m2;m3;m4];
alfa = [-2:2:12];
A = zeros(4,8);
CLa = zeros(1,8);
CDia = zeros(1,8);
for ii = 1:length(alfa)
n = ((alfa(ii)+1.2)*pi/180)*(mu.*k1);
A(:,ii) = m\(n.');
CLa(ii) = A(1,ii)*pi*9;
CDia(ii) = (CLa(ii)^2/(9*pi))*(1+3*A(2,ii)^2/A(1,ii)^2+...
5*A(3,ii)^2/A(1,ii)^2 + 7*A(4,ii)^2/A(1,ii)^2);
end
EDIT In response to your comment I replaced A(n) with A(n,ii) in the above code.
  6 Comments
Matt Fig
Matt Fig on 21 Apr 2011
Did you copy and paste the code exactly as I put it, because I ran the code and got no such error. All variables were created just fine.
ii is always an integer and never 0.493803. Something changed...
Jeffrey
Jeffrey on 21 Apr 2011
Running like a Porsche. Thank you Matt.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!