MATLAB Answers


How to store and then plot values in a for loop?

Asked by Tyler
on 5 Feb 2013

My goal: plot T1, T2, and T3 on a graph when the matrix is solved at each value of "i" from 0 to 50 for d. What my code currently does is solve a system at various values of "i" for three variables. the end result (aka the values I am trying to store) end up being T1, T2, and T3. The problem is, it only uses the last value of each of those, and not the entire spectrum of results. How can i store each result for those three values and plot them on a graph to show how they relate over time?

for d=0:.1:50;
B=[-40000; i; 0];
A=[1 1 1; 10 28 40; 144 -240 180];



No products are associated with this question.

1 Answer

Answer by Walter Roberson
on 5 Feb 2013

A = [1 1 1; 10 28 40; 144 -240 180];
dvals = 0:.1:50;
for K = 1 : length(dvals)
  d = dvals(K);
  i = d * -40000;
  B = [-40000; i; 0];
  T = A \ B;
  T1(K) = T(1);
  T2(K) = T(2);
  T3(K) = T(3);
plot( dvals, T1, '^', dvals, T2, 's', dvals, T3, 'p')


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!