Matlab, figures and for loops - matlab

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.

Related

How do I plot inside a for loop? Matlab

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

Data must be a single matrix Y or a list of pairs X,Y

I want to make two line plots Ckkk and Ckk appear on the same plot. The above error keeps occurring. I've checked with similar problem and none of the fixes have helped.
The code is as below
function cancer()
clear all;
clc;
t0=0;tend=60;nt=tend;
dt=(tend-t0)/(nt-1);
t=t0:dt:tend;
mambda=0.75;nambda=0.50;lambda=0.25;phi=0.0;psi=0.0;
%nu cell kill strength
%nu=zeros(length(t));
%inu= find(t>=40);
%nu(inu)=0.12;
%mu cell kill strength
%mu=zeros(length(t));
%inu = t>=40;
%mu(inu)=0.22;
%lu cell kill strength
%lu=zeros(length(t));
%inu= t>=40;
%lu(inu)=0.32;
C(1)=100;Ca=100;Cb=100;Ct=100;
Ctt=100;Cttt=100;Cx=100;Ckk=100;Ck=100;Ckkk=100;K(1)=300;
for i=1:length(t)-1
%Equation ?c
Cx(i+1)=Cx(i)+dt*(lambda*Cx(i));
%Equation ?c(1-c/K)with different ? values.
Ck(i+1)=Ck(i)+dt*(lambda*Ck(i)*(1-(Ck(i)/K(i))));
Ckk(i+1)=Ckk(i)+dt*(nambda*Ckk(i)*(1-(Ckk(i)/K(i))));
Ckkk(i+1)=Ckkk(i)+dt*(mambda*Ckkk(i)*(1-(Ckkk(i)/K(i))));
%Gompertz Growth Equations with different ? values.
C(i+1)=C(i)+dt*(-lambda*C(i)*log(C(i)/K(i)));
%Anti-tumor treatment induces a tumor cell kill with strength 0?nu?1.
%Ct(i+1)=Ct(i)+dt*(-lambda*Ct(i)*log(Ct(i)./K(i)))-nu(i).*Ct(i);
%Anti-angiogenic treatment
K(i+1)=K(i)+dt*(phi*C(i)-psi*K(i).*((C(i)).^(2/3)));
When I call the plot function, Error on plot occurs
plot(Ckkk,'b',Ckk,'r');
xlabel('Time In Days')
ylabel('Number of Cells')
end
Just like the error says, you need to specify the X coordinates if more than a single matrix of Y coordinates is provided. In your case, you have two, Ckkk and Ckk. If you need to plot those as two independent variables, one solution is below
figure; clf;
plot(t, Ckkk,'b', t,Ckk,'r');
Another solution is
figure; clf;
hold on;
plot(t, Ckkk,'b');
plot(t, Ckk,'r');
hold off;
In the later, you can actually skip t, because you are providing a single Y matrix to each plot function.
It is generally a good idea to check the documentation before you post a question here.

For loop in matlab plot

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:

plotting 2 variable of different size in matlab

Am trying to plot 2 variable of different size length in matlab GUI using push button,
but because the variables are of different length it will not work,is there a way i can make it to plot.
d= pdist([x,y,z],'euclidean') ; % value of my distance
dd= 1:10:d; % interval and end 'd' value
FSL=-120; %value of free space loss get from the GUI
DFSL= 1:10:FSL %interval and end at FSL value
plot(dd,DFSL)
The plot code didnt work coming back with an error "
Error using plot
Vectors must be the same lengths"
You can plot vectors of two different lengths, but not against each other. You have used the syntax
plot(x,y)
which means for every element in vector x, there should be a corresponding element in vector y. In your case, you do not have this, hence the error.
You can plot like this though:
plot(x)
figure;
plot(y)
If you are looking to plot them in a single plot, subplot will be useful.

MATLAB: Plotting different numerical approximation in one plot

I have the following matlab code for approximating a differential equation via the Euler-method:
% Eulermethod
a=0;
b=0.6;
Steps=6;
dt=(b-a)/Steps;
x=zeros(Steps+1,1);
x(1,1)=1;
y=zeros(Steps+1,1);
for i=1:Steps
x(i+1,1)=x(i,1)+dt*(x(i,1)*x(i,1)+1);
end
plot(x)
I want to be able to plot the solution plot for several different values of Steps in one plot and have the x-axis go from 0 to 0.6 instead of from for example 1 to 100 000 etc. Can this be done?
If you use the hold on command this will allow you achieve multiple plots on the same figure. Similarly, if you separate your data into x and y vectors, you can plot them against eachother by passing 2 vectors to plot instead of just one. For example
figure
hold on
for i=1:m
x = [];
y = [];
%% code to populate your vectors
plot(x,y)
end
You should now see all your plots simultanesously on the same figure. If you want x to be composed of n equally spaced elements between 0 and 0.6, you could use the linspace command:
x = linspace(0.0,0.6,n);
In order to distinguish your plots, you can pass an extra paramter to the function .For example
plot(x,y,'r+')
will plot the data as a series of red + symbols.
Plot can take more arguments: plot(x_axis,values, modifiers);
If x-axis is a vector of M elements, values can be a matrix of MxN elements, each of which are drawn with a separate color.