Plot line doesn't follow function

조회 수: 1 (최근 30일)
Pedro Almeida
Pedro Almeida 2023년 3월 7일
편집: Jan 2023년 3월 7일
The plot line I created doesn't follow the function, which should be an exponencial.
The following code, only the final for loop matters to the question:
format short
prod_total = [];
for j = 1:1000
x0 = 4;
m = 5;
vetor_aleatorio = [ ];
for k = 1:100
[u1, x0] = n_aleatorio(x0, k, m);
vetor_aleatorio(k) = u1;
end
x = rand;
if (x <= 0.2)
prod = 1;
elseif (x > 0.2 & x <= 0.6)
prod = 2;
elseif (x > 0.6 & x <= 1)
prod = 3;
end
prod_total = [prod_total, prod];
end
Unrecognized function or variable 'n_aleatorio'.
histogram(prod_total)
y = 2.8*rand + 0.8;
y = y*10;
y = fix(y);
y = y/10;
vet_pedidos(i)=y;
vet_servido = [];
x_total = [];
vet_total = [];
for i = 1:1000
x_exp = rand;
vet_servido = - (1/2) * log(1- x_exp);
vet_total = [vet_total, vet_servido];
x_total = [x_total, x_exp];
end
plot(x_total, vet_total, '-x');
  댓글 수: 2
Matt J
Matt J 2023년 3월 7일
Your code does not run (see above).
Pedro Almeida
Pedro Almeida 2023년 3월 7일
Yeah you are right, just use this:
for i = 1:1000
x_exp = rand;
vet_servido = - (1/2) * log(1- x_exp);
vet_total = [vet_total, vet_servido];
x_total = [x_total, x_exp];
end
plot(x_total, vet_total, '-x');

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

채택된 답변

Jan
Jan 2023년 3월 7일
편집: Jan 2023년 3월 7일
You determine the corret values, but draw them in random order. Sorting cleans the diagonalish lines:
x_total = zeros(1, 1000); % Pre-allocation for speed
vet_total = zeros(1, 1000); % Pre-allocation for speed
for i = 1:1000
x_exp = rand;
vet_servido = - (1/2) * log(1- x_exp);
vet_total(i) = vet_servido;
x_total(i) = x_exp;
end
[xs, index] = sort(x_total);
ys = vet_total(index);
plot(xs, ys, '-x');

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by