Plotting log x and y

1 次查看(过去 30 天)
Francis Wendell
Francis Wendell 2021-2-12
format long;
syms i imin n error y x h emin;
n = 30;
x = 0.5;
h = 1;
emin = 1;
for i =1:n
h = h*0.25;
y = [sin(x+h)-sin(x)]/h;
error(i) = abs(cos(x)-y);
i;
h;
y;
error;
TruncationError(i) = ((-1/6)*h^2*cos(x));
TotalError = ((-1/6)*h^2*cos(x)+error);
if error < emin
emin = error;
imin = i;
end
end
imin;
emin;
%hold on
%a = -log10(abs(error));
%b = log10(h);
%plot (a,b)
%plot (error);
%plot (TruncationError);
%plot (TotalError);
I'm new to matlab and am not sure how all the commands work. I am trying to plot error, truncation error, and total error all on the same graph. The x and y axis need to be what a and b are both log. It should look similar to the picture below numbers are different. Any guidance in the correct direction will be extremely helpful. Thanks.
  1 个评论
dpb
dpb 2021-2-12
编辑:dpb 2021-2-12
You've not saved the values of h in the loop to use to plot against -- but also note that at the end of your loop that h will be (1/4)^n --> (1/4)^30
>> (1/4^30)
ans =
8.6736e-19
>> log10(ans)
ans =
-18.0618
>>
so your log10(h) axis would go from 0 --> -19 instead of 0 --> 6
So unless the plot is mislabeled, it shows h went from ~10 to 10^6 instead.
There is also no need for symbolic variables here...

请先登录,再进行评论。

回答(1 个)

Rafael Hernandez-Walls
Why don't you treat like this:
n = 30;
x = 0.5;
h(1) = 1;
emin = 1;
for i =1:n
if i==1
h(1)=0.5;
else
h(i) = h(i-1)*0.25;
end
y = [sin(x+h(i))-sin(x)]/h(i);
error(i) = abs(cos(x)-y);
TruncationError(i) = ((-1/6)*h(i)^2*cos(x));
TotalError(i) = ((-1/6)*h(i)^2*cos(x)+error(i));
if error(i) < emin
emin = error;
imin = i;
end
end
loglog(h,TotalError,'r')
figure
loglog(h,TruncationError,'k')

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by