Plotting log x and y

조회 수: 2 (최근 30일)
Francis Wendell
Francis Wendell 2021년 2월 12일
답변: Rafael Hernandez-Walls 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
Rafael Hernandez-Walls 2021년 2월 12일
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