Plotting for loop with two outputs

조회 수: 3 (최근 30일)
Petch Anuwutthinawin
Petch Anuwutthinawin 2021년 6월 13일
댓글: KSSV 2021년 6월 14일
I need to plot a function with a two variable output using any method. The graph should plot an x range of 2-1000000. I have used a for loop which should plot the prewritten function L, which outputs two different variables. Both are below.
%%This is the function that works. It is for the collatz conjecture.
function [N, mx] = prob3_6(x)
X=x;%to initialise an X value that is not the input
N=0; %to initialise an N value that starts with 0 iterations.
mx=x; %create a definition for max in terms of x.
while X~=1; %while x is not 1, ends when x=1 if ever
if mod(X,2); % if x is odd
X=1+3*X; % do calculation for odd input
else
X=X./2; %even numbers divided by 2
end
N=N+1 %for each loop, N will add 1 iteration from zero
if X>mx; % if an X value happens to be greater than the set max, which is the input,
mx=X %it will override that value with the new max.
end
end
%% This is the code that does not work for plotting the function
for X= 2:1000000;
L=HW32P12Function(X);
end
figure(1)
loglog(X,L)
grid on
How do I use a this loop to plot the function that I have made?
  댓글 수: 1
LO
LO 2021년 6월 13일
isn't there a typo there ? HW32P12 ?
also as you plot you want either to use "cla" after "figure(1)" to refresh the plotting or use hold on to keep all plots. Or what would you like to do ?

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

채택된 답변

KSSV
KSSV 2021년 6월 14일
편집: KSSV 2021년 6월 14일
X= 2:1000000;
n = length(X) ;
L = zeros(1,n) ;
for i = 1:n
L(i) =HW32P12Function(X(i));
end
figure(1)
loglog(X,L)
grid on
  댓글 수: 2
Petch Anuwutthinawin
Petch Anuwutthinawin 2021년 6월 14일
I have tried this but L is not indexed at the end, is there a way to do that so it can plot?
KSSV
KSSV 2021년 6월 14일
Edited the answer.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by