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

1 view (last 30 days)
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.

Answers (1)

Roshni Garnayak
Roshni Garnayak on 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');

Community Treasure Hunt

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

Start Hunting!