1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: How do I create a matrix from a simple for loop?

  1. Nov 28, 2013 #1
    1. The problem statement, all variables and given/known data

    I am trying to create a code, in Matlab, that will describe the geometry of a PARSEC airfoil. The x coordinates for the upper and lower surfaces of the airfoil shape simply run from 0 to 1 (for a unit chord length). The z-coordinates (that describe the shape) are given by:

    z_upper = sum (a_n * x ^ ((n-1) / 2) )

    where the summation occurs over n = 1 to n =6. The coefficient a_n is determined by solving a system of linear equations - I think I have done this correctly.

    The lower surface z-coordinates are similarly defined by with a different set of coefficients.

    The way I have approached the problem is to try and create a matrix with 6 columns (for n = 1:6) and as many rows are there are x-coordinates. A vector containing all of the z-coordinates is then found by summing the values across each of the 6 columns for each row.

    However, Matlab is only returning a 7x6 matrix instead of a matrix with as many rows are there are x-coordinates. It is also returning an 'index out of bounds' when creating the vector of z-coordinates - I think this is just because the defined matrix is too small.

    My apologies if the solution is a simple one, I am new to coding.

    2. Relevant equations

    for n = 1:6

    z_upper = Ʃ a_n * x ^ ((n-1)/2)

    3. The attempt at a solution

    % Test Values

    x(1) = 0.01;

    x(2) = 8;

    x(3) = 12;

    x(4) = 0.43;

    x(5) = 0.12;

    x(6) = -0.8;

    x(7) = 0.23;

    x(8) = -0.020;

    x(9) = 0.1;

    x(10) = -0.004;

    x(11) = 0.003;

    x(12) = 0.0030;

    % x-coordinate range

    x_coord = 0:0.01:1;

    % upper surface coefficients (x_u = a_n)

    % Ax = B

    A_U = [ 1 1 1 1 1 1 ; x(4)^(1/2) x(4)^(3/2) x(4)^(5/2) x(4)^(7/2) x(4)^(9/2) x(4)^(11/2); 1/2 3/2 5/2 7/2 9/2 11/2;... (1/2)*x(4)^(-1/2) (3/2)*x(4)^(1/2) (5/2)*x(4)^(3/2) (7/2)*x(4)^(5/2) (9/2)*x(4)^(7/2) (11/2)*x(4)^(9/2);... -(1/4)*x(4)^(-3/2) (3/4)*x(4)^(-1/2) (15/4)*x(4)^(1/2) (35/4)*x(4)^(3/2) (63/4)*x(4)^(5/2) (99/4)*x(4)^(7/2);... 1 0 0 0 0 0];

    B_U = [x(10) + (1/2)*x(12); x(5); tan((2*x(2) - x(3))/2); 0; x(6); sqrt(x(1))];

    x_u = A_U\B_U;

    % z (upper surface) coordinates

    % z_upper = sum ( (x_u(n) * x_coord(n-1/2) )

    for j = 1:6

    for i = 1:length(x_coord)

    z_mat(i,j) = x_u(j) * x_coord(i) ^ ((j-1)/2);

    z_upper(i) = sum(z_mat(:,(i)));

  2. jcsd
  3. Nov 28, 2013 #2

    Simon Bridge

    User Avatar
    Science Advisor
    Homework Helper

    Sounds like you have not been careful to make sure that the various vectors are the right length.
    Check that you are not calling an index that does not exist.

    You seem to be under-using matlab - your approach makes it difficult to troubleshoot.
    There are better ways to achieve your aim in matlab by using matrix equations.
    i.e. you don't need to explicitly enter each member of a vector separately.

    Try troubleshooting with smaller vectors, and make assigned dimensions into a variable you declair at the start.
    i.e instead of j=1:6, do j=1:R and you have previously written R=6.

    Check the size of each of the vectors that are used to make the matrix ...
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted