Problem with a matlab question

Currently doing revision for a MATLAB module next year. Was attempting a question and was hoping for some help it.
I am asked to consider this Taylor series expansion:
S(x,n)=(x-1) - 1/2(x-1)^2 + 1/3(x-1)^3 - 1/4(x-1)^4 +...+ ((-1)^(n-1))*(1/n)*(x-1)^n
From this write a matlab function that, given the values of x and n, returns the value of S(x,n).
Obviously, I do not want someone to do the problem for me and just copy it. I would just like some advise and how to represent this as a script.
Thank you in advance for your time

 채택된 답변

Evan
Evan 2013년 8월 5일
편집: Evan 2013년 8월 5일

0 개 추천

There are multiple ways you can do this. The first involves a for loop and, while it might require more lines of code, is probably a more basic route to follow and might be beneficial for getting into the swing of MATLAB.
The second way, however, is much more compact and takes advantage of MATLAB's strengths: array operations. A hint: in order to do this, the element-by-element array operators (.* ./ .^) will be needed instead of the normal matrix arithmetical operators (* / ^).
For either method, what you are needing to do is the same: you want to apply some arithmetic operation to all integer values from 1 to n, then you want to add up the results.
If that's too vague or you think an example might help, just let me know.

댓글 수: 5

Joe
Joe 2013년 8월 5일
This is the kind of help I was looking for. As I have recently been introduced to this program I am just looking for help in how to tackle any mathematical problems. Thank you very much
Joe
Joe 2013년 8월 6일
Evan if you wouldn't mind helping me a bit more could you please provide an example ???
Jan
Jan 2013년 8월 6일
편집: Jan 2013년 8월 6일
This does not look smart. More compact and less prone to typos:
n = input('Value of n'):
S = 0;
for k = 1:n
S = S - (-1) ^ n / k .* (x - 1) .^ k;
end
And now you see the similarity to lain's answer.
Joe
Joe 2013년 8월 6일
Cheers Jan. Ended up with this:
for n = 1:m S = S + ((-1) ^ (n-1)) .* (((x - 1) .^ n)./n); end
From hand calculations and excel spreadsheet I know this is the right for loop for my example.
Jan
Jan 2013년 8월 6일
편집: Jan 2013년 8월 6일
Btw, the power operation is very expensive. Although runtime will most likely not matter in your case, this is the general approach to save time:
c = 1;
sgn = -1;
for n = 1:m
c = c * (x - 1);
sgn = -sgn;
S = S + sgn .* (c ./ n);
end
In the next step you can omit sgn also by injecting it into c: c = c * (1 - x)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Iain
Iain 2013년 8월 5일

0 개 추천

Its hard to give you help without giving you the answer to this one.
Option 1. Recursively call the script.
S = func(x,n)
function s = func(x,n) if n >0 s = "this term" + func(x,n-1); else s = "this term"; end
Option 2. Vectorize the summation
n = 1:n;
S = sum((-1).^(n-1) .* 1./n .*(x-1).^2 ))

댓글 수: 1

Joe
Joe 2013년 8월 5일
Yeah I had started to do that. What I had done is S = ((-1).^(n-1) .* 1./n .*(x-1).^2 )).
As this would just provide you with a function where it would solve the nth order. I'm assuming that by using the 'sum' function it will add up all orders up to the desired one. From the help you and Evan have provided I feel I have a much better understanding of the problem. Thank you

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

질문:

Joe
2013년 8월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by