Plotting Graphs in Matlab
2 views (last 30 days)
Show older comments
I am trying to make multiple plots of a function, which is an integral, with a constant in the integral that I want to vary. The coding I have tried is:
syms a f x y z
x=0:0.1:1
y=zeros(size(x));
for k=0:0.1:0.5
for i = 1:length(x);
func=a./(a.*0.046 + 8*10^-5 + (a.^4).*0.954 + k)^0.5;
y(i)=double(int(func,0,x(i)))
hold on
plot(y,x,'r')
end
xlabel('H_0 t')
ylabel('a')
title({'Scale factor a vs H_0 t.','Solution to Friedmann equation for multi component universe'})
end
So the changing constant is the factor k.
When I run this I get the graphs, but also a significant number of other lines, so clearly the "hold on" function isn't the one to use.
Any help would be much appreciated.
Thanks
2 Comments
Answers (1)
Stephen23
on 5 Aug 2016
Edited: Stephen23
on 5 Aug 2016
MATLAB is simply plotting it exactly what you are telling it to. You are getting confused about calculating vs plotting data, and which loops these should be in.
The calculation you are doing is irrelevant, so lets replace it with something simpler:
x = 0:0.1:1;
y = zeros(size(x));
for k = 0:0.1:0.5
for i = 1:length(x);
y(i) = 1+k^x(i); % fake calculation
disp(y)
hold on
plot(y,x,'rx-')
end
end
This will print the y values in command window on each loop iteration:
2 0 0 0 0 0 0 0 0 0 0
2 1 0 0 0 0 0 0 0 0 0
2 1 1 0 0 0 0 0 0 0 0
2 1 1 1 0 0 0 0 0 0 0
2 1 1 1 1 0 0 0 0 0 0
2 1 1 1 1 1 0 0 0 0 0
2 1 1 1 1 1 1 0 0 0 0
2 1 1 1 1 1 1 1 0 0 0
2 1 1 1 1 1 1 1 1 0 0
2 1 1 1 1 1 1 1 1 1 0
2 1 1 1 1 1 1 1 1 1 1
2 1 1 1 1 1 1 1 1 1 1
2.0000 1.7943 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
2.0000 1.7943 1.6310 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
2.0000 1.7943 1.6310 1.5012 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
2.0000 1.7943 1.6310 1.5012 1.3981 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
2.0000 1.7943 1.6310 1.5012 1.3981 1.3162 1.0000 1.0000 1.0000 1.0000 1.0000
etc.
Note how the first lines do not have all calculated y values (they are still full of the initial zeros), and yet you have already started to plot them on the very first loop iteration! This is why "it's as if the plot is returning to the y axis during the computation", because that is exactly what you are telling MATLAB to plot: a few calculated values and lots of (initialized) zeros!
The solution is simple: you need to calculate all of the y values first, and then plot after that. First calculate in your inner loop, then plot everything afterwards:
x = 0:0.1:1;
y = zeros(size(x));
for k = 0:0.1:0.5
for i = 1:length(x);
y(i) = 1+k^i; % fake calculation
end
disp(y)
hold on
plot(y,x,'rx-')
end
An even better solution is to move the plot out of the loops entirely: this has many advantages, for examples it will automatically color each line with a different color:
V = 0:0.1:0.6;
X = 0:0.1:1;
Y = NaN(numel(X),numel(V));
for ii = 1:numel(V)
for jj = 1:numel(X);
Y(ii,jj) = 1+V(ii)^X(jj); % fake calculation
end
end
plot(X(:),Y)
In future you need to learn how to track down bug yourself. Start by learning to look at the variables themselves: never rely on what you "know" a variables is like, always check it. A computer program does not run based on what you are thinking, it runs on the script that you have written, and the two are often not the same.
Reading the documentation is a really good idea too.
2 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!