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

2 次查看(过去 30 天)
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.

回答(1 个)

Roshni Garnayak
Roshni Garnayak 2020-1-10
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');

类别

Help CenterFile Exchange 中查找有关 Log Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by