Plot line doesn't follow function

2 visualizzazioni (ultimi 30 giorni)
Pedro Almeida
Pedro Almeida il 7 Mar 2023
Modificato: Jan il 7 Mar 2023
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 Commenti
Matt J
Matt J il 7 Mar 2023
Your code does not run (see above).
Pedro Almeida
Pedro Almeida il 7 Mar 2023
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');

Accedi per commentare.

Risposta accettata

Jan
Jan il 7 Mar 2023
Modificato: Jan il 7 Mar 2023
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');

Più risposte (0)

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Prodotti


Release

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by