Row vector using Leibniz series
90 ビュー (過去 30 日間)
古いコメントを表示
Greetings everyone, I am trying to create a function (without using loops) called LeibnizT that generates a row vector with the first n terms of the Leibniz series, the input argument is just n. My code is attached, thanks!
function y=LeibnizT(n)
y=(-1).^n/(2.*n+1)
end
0 件のコメント
採用された回答
John D'Errico
2023 年 1 月 4 日
編集済み: John D'Errico
2023 年 1 月 4 日
Think about it. How does that generate a VECTOR? In what you wrote, is n a SCALAR? (Yes)
y=(-1).^n/(2.*n+1)
So how will that create a vector as a result?
What tools can you think of that generate a vector, that do not have a for loop or a while loop? Surely you can think of some tools that will return a vector? In fact, that vector needs to be of length n. Take a shot. Show us what you can think of that gets you closer to what you need. If you do, then I'll see if I can give you another nudge in the correct direction, if necessary.
4 件のコメント
John D'Errico
2023 年 1 月 5 日
編集済み: John D'Errico
2023 年 1 月 5 日
Ok. That is getting closer. But still, you need to look carefuly at what you wrote. The argument is n. MATLAB cares about the case of its variables. So if you pass in n, then you cannot expect the variable named N to be defined. But you did this:
N=(1:N)
Where/how does MATLAB know what N is? You use it on the right hand side of that expression to define a variable named N. But you have passed in only n. Essentially, I think you are writing code that looks almost like what you want, but not thinking about how MATLAB wil see that code. What will a computer do? Yes, I know what you intended. But MATLAB will not. So look at EACH line you write. Are the variables defined? Are they what you think thy are? If necessary, test code fragments of your code, to see it they are doing as intended.
Then you wrote:
y=((-1).^(n-N))./(2.*(n-N)+1)
Where did the (n-N) come from? I'm not sure what you were thinking there. Anyway, you are getting closer.
First, what is the Leibniz series? Of course Leibniz has his name plastered all over mathematics, but I can only assume you want to compute the series:
1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ...
which taken as a limit of infinitely many terms, should reproduce pi/4. It is pretty slowly convergent though. The general term, as a function of N would be:
(-1)^N/(2*N+1))
starting with N at 0. Now lets look at the code you wrote, and see how to make it work. First, see that the general term you have written MUST start at N==0, because the first term in that alternating series is 1. (I could have started the series at N=1, but then the general term must be subtly altered.)
function y = LeibnizTerms(n)
N = 0:n;
y = (-1).^N./(2*N+1);
end
As you can see in the code, I dropped out many of the extra parens you had in there. I started the vector N at 0. And I used a semi-colon at the end of the lines, to prevent crap from being dumped to the command window. As well, I use N, NOT n-N, since I have no clue why you did that. Finally, 2*N was acceptable, instead of 2.*N, because MATLAB knows how to multiply a scalar times a vector. You could have used 2.*N too though. But I'm lazy, and like to save a dot when I can. ;-)
As a test, does that work? With n = 1e6, I get:
y = LeibnizTerms(1e6);
sum(y)
ans =
0.785398413397198
pi/4
ans =
0.785398163397448
As I said, this series approximation for pi/4 is not at all rapidly convergent. Molasses on a cold day moves faster.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Calculus についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!