Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

plot in a for loop

3 visualizzazioni (ultimi 30 giorni)
Maja lol
Maja lol il 6 Mag 2015
Chiuso: MATLAB Answer Bot il 20 Ago 2021
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 Commenti
Star Strider
Star Strider il 6 Mag 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
Maja lol il 15 Mag 2015
Modificato: Walter Roberson il 15 Mag 2015
Hello!
I think I got it, but i don't get any figure out of the values, would you like to help me again? Thank you!
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
n = [10 20 30 40 50 60 70 80 90 100];
for j = 10:10:100;
gauss_elim5(j);
j
y=ans
end
figure
plot(j,y)

Risposte (2)

Walter Roberson
Walter Roberson il 15 Mag 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.

Joseph Cheng
Joseph Cheng il 16 Mag 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.

Questa domanda è chiusa.

Community Treasure Hunt

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

Start Hunting!

Translated by