James Matthews
James Matthews

Reputation: 9

inputing multiple variables into a for loop in matlab

When i'm running this code in matlab, it is printing the g4 value into the array columns were g3 and its calculated value is suppose to be, as well as its own column. I was just wondering how to stop g4 being placed into g3's column and instead, print g3 and its value in the two arrays.

Cheers

v_meas = 0;
g1 = 1.09;
g2 = 0.9;
g3 = 0.93;
g4 = 0.85;
radius = 3.75;
K = 0.006;
m = g1;
g = g3;

for ii = 1 : 1 : 2
    v_meas = m*((radius^2)*pi)*K;
    ArrayOfDarceys(1,ii) = v_meas;
    ArrayOfGradients(1,ii) = m;
    v_meas = 0;
    m = g2;
    for jj = 3 : 1 : 4
         v_meas = g*((radius^2)*pi)*K;
         ArrayOfDarceys(1,jj) = v_meas;
         ArrayOfGradients(1,jj) = g;
         v_meas = 0;
         g = g4;
    end
end
ArrayOfDarceys
ArrayOfGradients

Upvotes: 0

Views: 49

Answers (1)

RPM
RPM

Reputation: 1769

I suspect you didn't intend to nest your for loops. Try this:

for ii = 1 : 1:  2
    v_meas = m*((radius^2)*pi)*K;
    ArrayOfDarceys(1,ii) = v_meas;
    ArrayOfGradients(1,ii) = m;
    v_meas = 0;
    m = g2;
end
for jj = 3 : 1: 4
     v_meas = g*((radius^2)*pi)*K;
     ArrayOfDarceys(1,jj) = v_meas;
     ArrayOfGradients(1,jj) = g;
     v_meas = 0;
     g = g4;
end

If I understand what you are trying to do you could significantly simplify your code though. There is, in fact, no need for any for loops:

ArrayofGradients = [1.09,0.9,0.93,0.85]
ArrayofDarceys = ArrayofGradients*((radius^2)*pi)*K

Upvotes: 2

Related Questions