What should be modified inside the for loop?

For each value of f_x_loop, I need x_a to take one of the five values from s_x_index for each iteration instead of one value for all the iteration.
s_x_index=[0.01 0.02 0.03 0.04 0.05]
for i = 1:length(s_x_index)
x_a = s_x_index(i);
f_x_loop = (F*x_a)/(s*Q)
f_x_loop_save(i) = f_x_loop(i)
end
Please let me know if the given information is not sufficient.
Thanks

 Accepted Answer

Change these lines:
f_x_loop = (F*x_a)/(s*Q)
f_x_loop_save(i) = f_x_loop(i)
into:
f_x_loop_save(i) = (F*x_a)/(s*Q)
in addition, put:
f_x_loop_save = zeros(numel(s_x_index),1);
before the for loop

5 Comments

I get this error.
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in test_contact_ellipse (line 392)
f_x_loop_save(i) = (F*x_a)/(s*Q)
Sara
Sara on 2 Jul 2014
Edited: Sara on 2 Jul 2014
what are the sizes of F, s, and Q?
it also appears you need to do is
x_a(i) = s_x_index(i);
or change the plot to be plot(s_x_index, f_x_loop_save, 'o') as you are overwriting x_a each time the loop occurs so when you plot you're plotting all f_x_loop_save at the last value of x_a from the loop.
At the start before you do their suggestion just do mine. what i suggest will get it so that all your values are not for one value on the X axis.
what was originally suggested is good practice as you're doing an un-necessary temporary calculation (b=a then c=b, why not just c=a since you aren't using b anywhere) and initialization of the array for memory optimization (faster to fill in matrix than keep adding to the end of an array).
Try this:
f_x = (F.*s_x_index)/(s*Q);
figure(5);
plot(s_x_index, f_x, 'o')
xlabel('Longitudinal creepage');ylabel('coefficient of adhesion')
hold on;

Sign in to comment.

More Answers (0)

Categories

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

Products

Asked:

on 2 Jul 2014

Edited:

on 6 Aug 2014

Community Treasure Hunt

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

Start Hunting!