Lines are a no show using loglog and a for loop

7 views (last 30 days)
So here is my code... ~~~~~~~~
function []= FFAplot(a,b,k)
P=.01;
T=1/P;
if length(a)==length(b) && length(b)==length(k)
for i=1:length(a)
Q=b(i)+(a(i)/k(i))*(1-(-log(1-P))^k(i));
loglog(T,Q)
xlim([1,100]);
hold on
grid on
end
else
error('Length of vectors not the same')
end
xlabel('Return period (yr)')
ylabel('Flood magnitude (cfs)')
legend('show')
end
By the way I used these as my a, b, k variables.
a=[2000 1000 4000];
b=[30000 40000 50000];
k=[-.3 -.1 -.4];
HOWEVER, the plot is not showing the lines.... It shows that there are line in the legend just not lines on the loglog graph. PLEASE SOME ONE HELP I AM GOING CRAZY.
<<
>>
%

Accepted Answer

Stephen23
Stephen23 on 29 Jan 2017
Edited: Stephen23 on 29 Jan 2017
The plot is not showing any lines because you did not plot any lines. You just plotted some individual, totally separate and disconnected points, one point on each loop iteration. How is MATLAB supposed to know that they should be connected? It is easy to show the points, if you really want to:
If you want to use MATLAB then you need to learn how to use vector and matrices, and not use ugly loops. With MATLAB you can perform operations on the whole vector or matrix: this is called vectorized code, then your task is simple because you can do you calculation without the ugly loop, and also by providing the inputs to loglog as a vectors then you tell MATLAB that it should join the points with lines:
function FFAplot(a,b,k)
P = 0.01;
%
assert(numel(a)==numel(b)&&numel(a)==numel(k),'Vectors must be same length')
%
Q = b + (a./k).*(1-(-log(1-P)).^k);
T = ones(size(Q))/P;
loglog(T(:),Q(:),'*-')
%xlim([1,100]);
grid on
%
xlabel('Return period (yr)')
ylabel('Flood magnitude (cfs)')
legend('show')
end
You might want to read this:

More Answers (1)

Jan
Jan on 29 Jan 2017
Edited: Jan on 29 Jan 2017
The "lines" are there, but you do not see them, because they are dots on the right border. Use this to see them:
loglog(T, Q, 'o')
  1 Comment
Jason Kolodziej
Jason Kolodziej on 29 Jan 2017
Thank you! It should be producing a line though... Hmmm... I will need to go back over my calculations.

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!