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

조회 수: 1 (최근 30일)
Burcu Yilmaz
Burcu Yilmaz 2020년 1월 2일
답변: Roshni Garnayak 2020년 1월 10일
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