开发者

MATLAB for loop plots

I have a quick question on making a simple plot in MATlab. I have done this before but for some reason it has stopped working, and the graph I want is no longer coming up.

I am not sure how much information is going to be needed in order for this question to be answered so please let me know if I should add more.

I have a system of ODE's and am using the following parameters in a loop as one of the variables.

A4bar = NaN;
for eta_p = 0.0:.1:7;
A3bar=x(size(t,1),3);
A4bar= [A4bar;A3bar];
hold on
plot(eta_p,A4bar,'-k','linewidth',1);

When I use the above code, my graph comes up with a bunch of plotted points 开发者_开发知识库that are not connected.

When I use my original code that worked in the past i used this.

A4bar = NaN;
for eta_p = 0.0:.1:7;
A3bar=x(size(t,1),3);
A4bar= [A4bar;A3bar];
place_holder_eta_p = 0:.1:7.1;
hold on
plot(place_holder_eta_p,A4bar,'-k','linewidth',1);

The error I get for my original code is that the vector's are not equal. I have tried setting the place_holder_eta_p to match with eta_p, and that didn't work. It used to work with just the above code, so I am not sure what is going on.

Any idea's or suggestions? Maybe more importantly, let me know if there is any additional information I should be providing.

Thanks everyone.


I suspect you don't need a for loop to do what you want to do. In fact, it's because you are plotting each individual point as its own plot that you get unconnected points in your graph. In any case, you haven't shown how the changing variable eta_p is used within the loop.

The basic structure of what your code should look like is this:

eta_p = 0:0.1:7;
N     = length(eta_p);
A4bar = Zeros(1, N);       % same length as eta_p
% set up your other variables

% calculate the values in A4bar
% it might look like
% for i = 1:length(eta_p)
%     A4bar(i) = interesting_function(eta_p(i), A3bar, x, t);
% end

plot(eta_p, A4bar, '-k', 'linewidth', 1);

I don't know how to set up your other variables A3bar, x, t, and I can't tell what you need in that middle section to calculate the values of A4bar without any additional information.

The important things to note are

  1. eta_p and A4bar are both size [1 N] (required for plot command)
  2. The plot command happens once outside any loop, not inside it, and after you've calculated all the values of A4bar.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜