I have a Maclaurin Series, how do I calculate it?

5 次查看(过去 30 天)
The Series is:
log(1-x) = -x/1 - x^2/2 - x^3/3 - x^4/4...
I need to write a script that prompts the use to enter a value for x, and calculate it to the first 15 terms.
My instructor has not gone over this in his notes so I have know idea what to do...

回答(4 个)

Walter Roberson
Walter Roberson 2015-8-9

Star Strider
Star Strider 2015-8-9
A one-line version:
log_1_minus_x = @(x,n) sum(-x.^(1:n)./(1:n));

Image Analyst
Image Analyst 2015-8-9
Here's a snippet that may be helpful for you to ask for the value of x and the number of terms to sum:
% Ask user for two floating point numbers.
defaultValue = {'45.67', '78.91'};
titleBar = 'Enter a value';
userPrompt = {'Enter x : ', 'Enter number of terms: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Convert to floating point from string.
usersValue1 = str2double(caUserInput{1})
usersValue2 = str2double(caUserInput{2})
% Check for a valid number.
if isnan(usersValue1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
usersValue1 = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
A for loop is simply
theSum = 0
for k = 1 : numberOfTerms
theSum = theSum + ...........
end
You should be able to take it from there. Just put in the expression for the kth term.

Eram Khan
Eram Khan 2021-12-28
编辑:Image Analyst 2021-12-28
h= input('enter number')
n= input('enter iteration')
m=0;
s=0;
while m<=n
K = (h/fact(m)) ^ m;
s = s + k;
m = m + 1;
end
Disp(s)
-------- fact is a function doing factorial for any number, such as "factorial()" which is a built-in function of MATLAB.
It is for exponential Maclaurin series

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by