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

조회 수: 6 (최근 30일)
Mark Dillon
Mark Dillon 2015년 8월 9일
편집: Image Analyst 2021년 12월 28일
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