Plotting for a wide range of values and Log-Log Scale

2 vues (au cours des 30 derniers jours)
Burcu Yilmaz
Burcu Yilmaz le 2 Jan 2020
Hello!
Here is my code,
for dt = 1:10
x=5;
x_arr=[];
x_arr(1)=x;
for i=1:10/dt
xnew=x*dt;
x=xnew;
x_arr(i+1)=xnew;
end
Difference=abs(x_arr(1)-x_arr(end))
hold on
plot(dt,Difference,'o')
end
hold off
ylabel('Difference between initial and final x value')
xlabel('dt')
I want to run this code for small dt values dt=10^-6:10^0
I know that in order to get a reasonable plot I should use loglog scale. However, I couldn't do that. Can you help me to understand how to use loglog scale for my code.
Thank you in advance for your help.

Réponses (1)

Roshni Garnayak
Roshni Garnayak le 10 Jan 2020
If you attempt to plot the data using ‘loglog’ with ‘hold on’ enabled, the data plots as a linear plot. You can store the ‘Difference’ and ‘dt’ values in arrays and plot the data outside the for loop. The code can be modified in the following way:
Difference = [];
DT = [];
for dt = 1:10
x=5;
x_arr=[];
x_arr(1)=x;
for i=1:10/dt
xnew=x*dt;
x=xnew;
x_arr(i+1)=xnew;
end
Difference = [Difference, abs(x_arr(1)-x_arr(end))];
DT = [DT, dt];
end
loglog(DT, Difference, '-o');

Catégories

En savoir plus sur Log Plots 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!

Translated by