Plotting log x and y
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
dpb
le 12 Fév 2021
Modifié(e) : dpb
le 12 Fév 2021
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...
Réponses (1)
Rafael Hernandez-Walls
le 12 Fév 2021
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')
0 commentaires
Voir également
Catégories
En savoir plus sur Calendar dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!