getting Complex double in matlab while excel solves it

I had this formula in excel few years ago with less variation of WL, but now want to have a bigger range and put it into matlab and it gives me Complex double and the curve is totally different from what I expect or that came out in excel, can anyone help me here
WL=0:1E-6:4e-3;
SS=0.06977+7.0625*(1-exp(-(WL-0.26053)./0.15994)).^2.28411.*exp(-(WL-0.26053)./0.2285);

 採用された回答

Stephen23
Stephen23 2019 年 6 月 15 日
編集済み: Stephen23 2019 年 6 月 15 日

0 投票

(1-exp(-(WL-0.26053)./0.15994)).^2.28411
%
% ^^^^ ^ positive values (around 5)
%^^^ ^ negative values (around -4)
% ^^^^^^^^^ fractional power of negative values.
What do you expect a fractional power of negative values to give you?
>> (-4).^2.28411
ans = 14.884 + 18.473i

5 件のコメント

Asliddin Komilov
Asliddin Komilov 2019 年 6 月 15 日
編集済み: Asliddin Komilov 2019 年 6 月 15 日
I know why its happening, the trouble is I am getting Complex double for the whole range of the outputs even for positive values.
Stephen23
Stephen23 2019 年 6 月 15 日
" the trouble is I am getting Complex double for the whole range of the outputs even for positive values"
Please show an example of this: I would like to see a positive value raised to a positive fractional power that returns a complex result.
Stephen23
Stephen23 2019 年 6 月 15 日
Asliddin Komilov's "Answer" moved here:
if you copy the following to the command window you will see that data will come out in complex, resulting in a plot as in 'screen2', at the same time similar calculated data gives a different curve ('screen1') in excel.
WL=1E-3:1E-3:4;
SS=0.06977+7.0625*(1-exp(-(WL-0.26053)./0.15994)).^2.28411.*exp(-(WL-0.26053)./0.2285)
Stephen23
Stephen23 2019 年 6 月 15 日
編集済み: Stephen23 2019 年 6 月 15 日
As I explained in my answer, you are calculating the exponent of a negative number using a fractional power, which produces a complex result. Taking a look at your example:
>> WL=1E-3:1E-3:2;
>> numel(WL)
ans = 2000
>> X = 1-exp(-(WL-0.26053)./0.15994);
>> Y = Y.^2.28411;
>> find(X(:)<0) % indices of the negative values!
1
2
3
4
... lots of sequential indices here
259
260
>> find(imag(Y(:))>0) % indices of where negative values -> complex result.
1
2
3
4
... lots of sequential indices here
259
260
You can easily avoid taking the exponent of negative values using max:
>> Z = max(0,1-exp(-(WL-0.26053)./0.15994)).^2.28411
>> find(imag(Z)>0)
ans = []
Or set those values to NaN, or whatever suits your algorithm.
"getting Complex double in matlab while excel solves it"
In fact your screenshots make it clear that MATALB solves it (correctly, giving complex results for those negative values), whereas Excel just returns a an error message.
Asliddin Komilov
Asliddin Komilov 2019 年 6 月 15 日
it worked, thanks

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by