我有这段用IDL写的代码
for i=n-1,0,-1 do y = TEMPORARY(y) * x + c[i]
我想了解如何在Matlab中实现。描述说这个函数返回
c[0] + c[1]*x + c[2]*x^2 + ...
我尝试在Matlab中手动执行此操作,但我的答案与使用IDL的答案不同。提前谢谢。
修改:
我在第三次编辑中添加了代码,使其缩短了一点。
编辑2:
编辑3:
这是我SEDfinal.m
中的一段代码,它给我提出了问题
wl_uvb = [3030.8; 3031.0; 3031.2; 3031.4; 3031.6; 3031.8; 3032.0; 3032.2; 3032.4; 3032.6; 3032.8; 3033.0; 3033.2; 3033.4];
f_uvb = [0.3539; 0.2284; 0.1788; 0.4813; 0.1783; -0.2679; 0.2853; 0.5093; -0.1204; 0.2237; 0.1706; -0.2036; 0.1912; 0.2150];
uvbflux_o1 = ccm_unred(wl_uvb,f_uvb,0.1,3.1);
ccm_unred
的作用是什么,
x = 10000./wl_uvb;
npts = length(x);
a = zeros(npts,1);
b = zeros(npts,1);
而不是
good = find(1.1 <= x & x < 3.3);
ngood = length(good);
if ngood > 0
y = x(good) - 1.82;
c1 = [1, 0.104, -0.609, 0.701, 1.137, -1.718, -0.827, 1.647, -0.505];
c2 = [0, 1.952, 2.908, -3.989, -7.985, 11.102, 5.491, -10.805, 3.347];
a(good) = polyIDL(y, c1);
b(good) = polyIDL(y, c2);
end
这就是问题的开始。 IDL poly
功能是
% OUTPUTS:
% POLY returns a result equal to:
% C[0] + c[1] * X + c[2]*x^2 + ...
FUNCTION POLY,X,C
compile_opt idl2
on_error,2
N = N_ELEMENTS(C)-1 ;Find degree of polynomial
% Special case for N=0. Be sure to return a result of the
% same type and array dimensions as X.
if (n eq 0) then $
return, x*0 + c[0]
Y = c[n]
for i=n-1,0,-1 do y = TEMPORARY(y) * x + c[i]
return,y
end
以及我在matlab中做的事情
function y = polyIDL(x,c)
n = length(c) - 1;
if n == 0
y = x*0 + c(1);
end
% c(1) + c(2)*x + c(3)*x^2 + c(4)*x^3 + c(5)*x^4 + c(6)*x^5 + c(7)*x^6 +
% c(8)*x^7 + c(9) * x^8
if n > 0
for i=1:n
z(i) = c(i).*x(i).^(i-1);
end
end
y = z(i);
end
我确实在matlab中更改了代码,因为现在我正在处理更多的值。结果是如此不同
matlab: All values are [n]*10^-05
0.0807, 0.0521, 0.0408, 0.1097, 0.0406, -0.0611, 0.0650, 0.1161, -0.0274, 0.0510, 0.0389, -0.0464, 0.0436, 0.0490
idl:
0.59040940, 0.38102922, 0.29827669, 0.80289233, 0.29742825, -0.44688237, 0.47589615, 0.84951931, -0.20082417, 0.37311712, 0.28454325, -0.33957571, 0.31888679, 0.35857213