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 回表示 (過去 30 日間)
古いコメントを表示
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')
0 件のコメント
採用された回答
Walter Roberson
2016 年 2 月 8 日
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 件のコメント
Walter Roberson
2016 年 2 月 8 日
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
2016 年 2 月 9 日
Is there a reason you did not copy the code I gave that shows how to save the points?
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Graphics Performance についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!