Row vector using Leibniz series
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Lewis HC
am 4 Jan. 2023
Kommentiert: Lewis HC
am 9 Jan. 2023
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 Kommentare
Akzeptierte Antwort
John D'Errico
am 4 Jan. 2023
Bearbeitet: John D'Errico
am 4 Jan. 2023
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 Kommentare
John D'Errico
am 5 Jan. 2023
Bearbeitet: John D'Errico
am 5 Jan. 2023
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.
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!