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

2 ビュー (過去 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 Center および File ExchangeLog Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by