Info
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
plot in a for loop
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Here is the function I'm gonna use later:
function [] = gauss_elim2 (n)
A = rand(n,n);
b = rand(n,1);
for j=1:20,
tic
x = A\b;
times(j)=toc;
end
mean(times)
I want to make a plot but it doesn't work:
valuey = zeros(0,90);
for i=0:10:90;
valuey(i)=gauss_elim2(i)
end
plot(i, valuey (i));
2 comentarios
Star Strider
el 6 de Mayo de 2015
To begin with, your ‘gauss_elim2’ function is not returning any outputs. If you want it to return ‘x’ (a column vector), you have to tell your function to do that, and you have to define your ‘valuey’ as a matrix to which you add a column at each iteration. Finally, in the ‘i’ loop, define ‘i’ outside the loop and then reference it appropriately in the loop by another name, because it contains 0, so you will not be able to use it as a loop counter. Note also that rand(0) is going to be an empty matrix and rand(0,1) an empty vector, so ‘x’ for that iteration is also going to be empty as well.
I am not listing this as an Answer because it is not one. All the problems I have mentioned are relatively easy for you to solve.
Maja lol
el 15 de Mayo de 2015
Editada: Walter Roberson
el 15 de Mayo de 2015
Respuestas (2)
Walter Roberson
el 15 de Mayo de 2015
function [x, y] = gauss_elim5 (n)
A = [n;n];
b = [n;1];
j=1:20;
for i=j
tic
x = A\b;
times(i)=toc;
end
y=mean(times) ;
end
Then
n = [10 20 30 40 50 60 70 80 90 100];
jvals = 10:10:100;
for j = jvals
[x, y(j)] = gauss_elim5(j);
end
figure
plot(jvals,y)
I only repaired plotting as that is the part you asked about. I did not repair the fact that your plot is not meaningful, and I did not repair the fact that you very likely broke your function between your original post and your revision.
0 comentarios
Joseph Cheng
el 16 de Mayo de 2015
I would highly suggest you go back and read the matlab documentation on indexing arrays or how to make arrays. Here are my comments on your code:
function [x, y] = gauss_elim5(n)
A = [n;n];
b = [n;1];
j= 1:20
for i=j
tic
x = A\b; %you're not saving anything here.
%in each iteration you are overwriting x
times(i)=toc;
end
y=mean(times);
end
and then in the script
n = [10 20 30 40 50 60 70 80 90 100];
for j = 10:10:100;
gauss_elim5(j); %you are not storing the output
j %you are displaying j so in the next line ans=j
y=ans %since you are using ans you are only getting the value of the previous line which you have as j.
end
figure
plot(j,y) %now you just plotted the last iteration of the for loop which is just the point (100,100).
That said i'm still confused to what this is trying to accomplish so here is my stab at trying to fix it.
adjustments made to the script:
n = [10 20 30 40 50 60 70 80 90 100];
for j = 1:length(n);
[~,y(j)]=gauss_elim5(n(j));
end
figure
plot(n,y)
with no clue on what you want to do for x in the function.
0 comentarios
La pregunta está cerrada.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!