I want to set a range of variable to three or four places of decimals

7 ビュー (過去 30 日間)
WooYong Lee
WooYong Lee 2020 年 11 月 25 日
コメント済み: Walter Roberson 2020 年 11 月 26 日
How can i set a range of variable to three or four places of decimals? I'm going to write a function of the variable and float a graph. I want to find optimal value under specific function with the variable.

採用された回答

John D'Errico
John D'Errico 2020 年 11 月 26 日
Are you asking to create a variable that only carries 3 or 4 digits of precision? You can't choose to set an arbitrary precision, unless you use a tool like my HPF, but that would take far more effort on your part to learn than it would help you, and it has limits.
At best, you can use single precision, giving you roughly 8 digits. And Cleve Moler has posted some tools for shorter prescision, but those tools will also be limited in which functions can use them.
Or, are you asking how to plot a function over a domain that varies over 3 or 4 orders of magnitude for a plot?
That is far easier, since you can use tools like logspace, or semilogx, etc. And of course, you can always work using logs.
  3 件のコメント
WooYong Lee
WooYong Lee 2020 年 11 月 26 日
in logspace/semilogx, can I set a range of variable's order of magnititude in integer range?
Walter Roberson
Walter Roberson 2020 年 11 月 26 日
logspace(A, B, N) is the same as 10.^(linspace(A,B,N))
For example,
format long g
logspace(-2, 5, 3)
ans = 1×3
0.01 31.6227766016838 100000
10.^linspace(-2,5,3)
ans = 1×3
0.01 31.6227766016838 100000
You can do things like:
mat2str(10.^(-2:5))
ans = '[0.01 0.1 1 10 100 1000 10000 100000]'
but that does not restrict the range of the variable for optimization routines.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 11 月 26 日
I want to find optimal value under specific function with the variable.
The MATLAB minimizers such as fmincon() and ga() all accept an options parameter, and the options parameter permits you to set a function tolerance -- so that you could stop searching when the function is changing less than a certain value, instead of comtinuing on to try to find the absolute best position.

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by