Effacer les filtres
Effacer les filtres

Plot line doesn't follow function

4 vues (au cours des 30 derniers jours)
Pedro Almeida
Pedro Almeida le 7 Mar 2023
Modifié(e) : Jan le 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 commentaires
Matt J
Matt J le 7 Mar 2023
Your code does not run (see above).
Pedro Almeida
Pedro Almeida le 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');

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 7 Mar 2023
Modifié(e) : Jan le 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');

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by