Filter löschen
Filter löschen

the code below I'm trying to plot a loglog graph, but everytime I run the code I keep getting an empty graph, then when i use the brush i can see the points. can anyone help

1 Ansicht (letzte 30 Tage)
n=10;
x=0.2;
for i=1:n
h=1/i;
cf=dot([1 -2 1],[(cos(pi*(x+h))) cos(pi*x) cos(pi*(x-h))])/h^2;
rf=dot([2 -5 4 -1],[cos(pi*x) ;cos((pi*(x+h))) ;cos((pi*(x+2*h))); cos(pi*(x+3*h))])/h^2;
ec=abs((-pi^2*cos(pi*x))-cf);
er=abs((-pi^2*cos(pi*x))-rf);
loglog(h,ec,'g');
hold on
loglog(h,er,'r');
hold on
end
xlabel('h')
ylabel('ec + er')

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 8 Feb. 2016
You are only plotting one point at a time so you will not get connecting lines. But you have not specified any marker shape or marker size so you cannot see the markers.
Are you trying for lines or for a scatter plot?
  3 Kommentare
Walter Roberson
Walter Roberson am 8 Feb. 2016
At the time you do
loglog(h,ec,'g');
your h is a scalar (1/i) and your ec is a scalar calculated just above. You are therefore using loglog() of one scalar against another. That plots one point. However, when you use plot() without specifying a data marker, the points are not plotted... so you get no visible output. If you had used
loglog(h,ec,'*g');
you would have gotten visible individual points.
But you want lines. To get lines you should store the computations and then plot() the results.
n=10;
x=0.2;
for i=1:n
h(i) = 1/i;
cf = dot([1 -2 1],[(cos(pi*(x+h(i)))) cos(pi*x) cos(pi*(x-h(i)))])/h(i)^2;
rf = dot([2 -5 4 -1],[cos(pi*x) ;cos((pi*(x+h(i)))) ;cos((pi*(x+2*h(i)))); cos(pi*(x+3*h(i)))])/h(i)^2;
ec(i) = abs((-pi^2*cos(pi*x))-cf);
er(i) = abs((-pi^2*cos(pi*x))-rf);
end
loglog(h, ec, 'g');
hold on
loglog(h, er, 'r');
hold off
xlabel('h')
ylabel('ec + er')
Walter Roberson
Walter Roberson am 9 Feb. 2016
Is there a reason you did not copy the code I gave that shows how to save the points?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Performance finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by