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

Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 5일
What is your problem? What are the other lines?
mike halls
mike halls 2016년 8월 5일
On the output grpah, the required graphs are there.....however, there are also a large number of almost horizontal lines running from the graphs back to the y axis - it's as if the plot is returning to the y axis during the computations. These do not appear to the right of the graphs. So the effect is that I can see all of the reuired curves, but additionally between those and the y axis are a load of almost horizontal lines.

댓글을 달려면 로그인하십시오.

답변 (1개)

Stephen23
Stephen23 2016년 8월 5일
편집: Stephen23 2016년 8월 5일

0 개 추천

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

mike halls
mike halls 2016년 8월 5일
Stephen - thanks so much! You're a star!!
Yes, I see now what I was doing wrong, and I've got it working properly now. As you will have realised I am pretty new to this, so make lots of stupid mistakes. Anyway, thanks again for taking the trouble to help.
Stephen23
Stephen23 2016년 8월 5일
편집: Stephen23 2016년 8월 5일
There is nothing wrong with making mistakes, as long as you learn from them.
Please accept my answer if it resolves your original question.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

태그

질문:

2016년 8월 5일

편집:

2016년 8월 5일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by