Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

How to convert the following program to vector operation (not using for loop)

2 Ansichten (letzte 30 Tage)
TY
TY am 18 Nov. 2014
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
function y= natural_log(x,n)
if (abs(x)>=1)||(n<=0)
disp ('Error')
else if ((abs(x)<1)&&(n>0))
y=x;
q=1;
for a=2:n
q=q*(-1);
y=y+q*((1/a)*(x^a));
end
end
end

Antworten (1)

Roger Stafford
Roger Stafford am 18 Nov. 2014
elseif
y = sum(-(-x).^(1:n)./(1:n);
...
You should be aware that this series sum will approach log(1+x), not log(x). Also be aware that the original for-loop might be faster than the vectorized form for large n.

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by