I'm trying to plot a straight line from a point in x to different values of t, thereby making a line in a for loop. But I see no lines generated in my figure in MATLAB
Following is my code:
t=linspace(0,8,11)
xs=(1.+t).^0.5
x0=xs./(1.+t)
m=size(t)
n=max(m)
hold on
for k=1:n
plot(x0(k),t(1:k),'-')
hold on
end
Thanks
You do not need the loop to perform the plot.
plot(x0,t,'-')
Will work just fine! Unless you were attempting to plot points...use scatter() for that:
scatter(x0,t)
plot() and scatter() (and most of Matlab's functions) are meant to be used with vectors, which can take some time to get used to if you are used to traditional programming languages. Just as you didn't need a loop to create the vector x0, you don't need a loop to use plot().
You are adding one point in Y axis along a line in X Axis use this code
t=linspace(0,8,11)
xs=(1.+t).^0.5
x0=xs./(1.+t)
m=size(t)
n=max(m)
hold on
for k=1:n
plot(x0(1:k),t(1:k),'-')
hold on
end
for more fun and see exactly how for is performed use this for loop
for k=1:n
pause('on')
plot(x0(1:k),t(1:k),'-')
hold on
pause(2)
end
Related
Inside a while loop, I have some function that creates all the neccesary y-values for the plot I want to make. After all the y-values are done I want my program to plot the dat(while still inside the loop), but the plot can't be made because the data won't come out until the end of the loop.
Is there anyway to do this?
Basically my code is(and I'm just going to for the first case here)
while c~=3
c=menu('a','b','c')
switch c
case 1
for
%function that creates y-values
end
plot(x,y)
end
end
As I said; I get out all the data at the end of the loop, which is stored in the workspace. Meaning that when I run it a second time, it works fine.
But I want to know how to make it work the first time.
for Continuous line plot you can use drawnow and here it is explained how to do this (remember to use pause(.) if you want to visualize the changes "real-time".
for retain current plot when adding new plots use hold on as it is explained here
if you want to open different windows for every different plot you can use something like:
ii=1;
while ...
...
figure(ii)
plot(x,y)
ii=ii+1;
...
end
but be careful with the last one: if you have a big number of plots you can have some problem
I was trying to plot the graphs in the interval [60, 110] of a sequence of ten functions f_i(t) in the same figure, whose definitions will be clear from the code below:
figure
i=1;
while i<=10;
P_i=abs(sin(i));
r_i=0.005*abs(cos(i.^2));
y=#(i,t)P_i*exp(r_i*t)/(1+P_i*(exp(r_i*t)-1));
% disp([(y(i,67));(y(i,68));(y(i,69))]');
s=linspace(60,110,51);
i=i+1;
continue;
end;
I ran the piece of code and it works. As you see, I could create one single function y(i,t) where i is an integer 1<=i<=10, and t is a continuous variable. But how can I construct ten functions y_i of a single variable t in the above code and plot their graphs in the same figure, so it'll consist of the graphs of ten functions together. How can I achieve it?
Thanks
First of all some minor modifications on your code. P_i and r_i are functions as well, so define them as functions. This way you only need to define the function once. Besides using a loop as you did it easily causes errors. i is the imaginary unit and you did not initialise it. Use a for loop instead and avoid i
P_i=#(i)abs(sin(i));
r_i=#(i)0.005*abs(cos(i.^2));
k=1:10;
t=60:101;
y=#(i,t)P_i(i)*exp(r_i(i)*t)/(1+P_i(i)*(exp(r_i(i)*t)-1));
To plot a Matrix with all values is required:
M=nan(numel(ik),numel(it));
for ik=1:numel(k)
for it=1:numel(t)
M(ik,it)=y(k(kx),t(it));
end
end
And finally Plot it. I did not really understand if you want plot(k,M) or plot(t,M) but one should be the right.
I am trying to plot the following simple function; $y=A.*x$ with different A parameter values i.e. A=0,1,2,3 all on the same figure. I know how to plot simple functions i.e. $y=x$ by setting up x as a linspace vector so defining x=linspace(0,10,100); and I know that one can use the hold command.
I thought that one could simply use a for loop, but the problem then is getting a plot of all the permutations on one figure, i.e. I want a plot of y=t,2*t,3*t,4*t on the same figure. My attempt is as follows:
x=linspace(0,10,100);
%Simple example
Y=x;
figure;
plot(Y);
%Extension
B=3;
F=B*x;
figure;
plot(F);
%Attempt a for loop
for A= [0,1,2,3]
G=A*x;
end
figure;
plot(G);
This is how I would plot your for loop example:
figure;
hold all;
for A=[0,1,2,3]
G=A*x;
plot(G);
end
figure creates a new figure. hold all means that subsequent plots will appear on the same figure (hold all will use different colours for each plot as opposed to hold on). Then we plot each iteration of G within the loop.
You can also do it without the loop. As with most things in Matlab, removing the loop should give improved performance.
figure;
A=[0,1,2,3];
G=x'*A;
plot(G);
G is the outer product of the two vectors x and A (with x having been transposed into a column vector). plot is used to plot the columns of the 100x4 matrix G.
Hello i am having some problems understading basic plotting in matlab.
I can understand why you would use a for loop when plotting data?
Can anybody explain this to me?
I am making a simple linear plot. Is there any reason this should be inside a loop
If you are making a simple plot there is virtually no reason to use a loop.
If you check doc plot you will find that plot can take some vectors as input, or even matrices for more interesting situations.
Example:
x=0:0.01:1;
y=sin(x);
plot(x,y)
No there is no need in Matlab to use a for loop for plotting. If you are looking for a simple linear plot your code could look like this:
x=1:100;
y=3*x+4;
plot(x,y)
As you see there is no for loop needed. Same goes for nearly all plots and visualization.
A possible reason to use a for loop to plot thing may be having several data to plot in a single matrix. Say you have two matrix Ax (MxN) and Ay (MxN) where N the length of each data and M is the amount of different data wanted to plot. For example like in this case N is 201 and M is 3:
% Create Ax and Ay
Ax=meshgrid(0:0.1:20,1:3);
Ay=zeros(size(Ax));
% Sinusoidals with different frequencies
for k=1:3
Ay(k,:)=sin(k.*Ax(k,:));
end
% create colours
colorVec = hsv(3);
% Plot
hold on
for k=1:3
plot(Ax(k,:),Ay(k,:),'Color',colorVec(k,:))
end
hold off
You get:
I'm very new to MATLAB and I was trying to display a real time plot of some calculations. I have an N sized vector and I work with m values at a time (say m = N/4), so I want to plot the first m values and then as soon as the second m values are calculated have them replace the first plot.
My approach was as follows:
for i=1:N,
...
//compute m
...
plot(m);
end;
but it fails to update the plot in every loop and waits for all the loops to finish to plot the data. My question is: Should I use another function instead of plot or could I add some delay in each loop?
I think there must be a way I'm not aware of for updating the plot instead of re-plotting it every time.
As Edric mentioned, you'll definitely want to include a drawnow command after the call to plot to force an update of the graphics. However, there is a much more efficient and smoother method to animate plots that doesn't involve recreating the entire plot each time. You can simply initialize your plot, capture a handle to the plot object, then modify the properties of that object in your loop using the set command. Here's an example:
hLine = plot(nan); % Initialize a plot line (which isn't displayed yet
% because the values are NaN)
for i = 1:N % Loop N times
...
% Compute m here
...
set(hLine, 'YData', m); % Update the y data of the line
drawnow % Force the graphics to update immediately
end
In addition, before your loop and after the call to plot you can set a number of axes properties, like the axes limits, etc., if you want the axes to stay fixed and not change their appearance with each new vector m that is plotted.
You can add a call to DRAWNOW to force the plot to update. See the reference page. Note that DRAWNOW causes the graphics event queue to be flushed, which may cause callbacks etc. to be executed.