Could someone teach me how to add numbers in a series?

12 visualizaciones (últimos 30 días)
Mark
Mark el 14 de Mzo. de 2013
Currently my engineering professor has done very little to teach us anything about how to use matlab, and I feel like I am stumbling through the dark on some of the more complex problems he is giving us. Enough of that! My current problem is using the Leibniz Formula. He wants us to write a program that will add a given number of terms together from 1:n.
ex: The sum of the terms to add is: 3
(4-(4/3)+(4/5))=3.46666666667
I would really like a push in the right direction, not an answer.
Thanks!

Respuestas (2)

Wayne King
Wayne King el 14 de Mzo. de 2013
Welcome to MATLAB! Spend some time reading the MATLAB Getting Started guide. That will help you a lot.
In MATLAB, the key is to represent a sequence of numbers like you have above in an array
X = [4 -4/3 4/5];
Now many MATLAB functions operate naturally on arrays of various sizes and dimensions. For example, read the help for sum()
Y = sum(X)

Carlos
Carlos el 14 de Mzo. de 2013
Consider the Euler series to sum pi:
((pi^2)/6)=∑ (1/n^2),then pi= sqrt(6*∑ (1/n^2))
To implement the above method in Matlab you cand do
>> aux=0; %initialize a variable to zero
for k=1:1:10000 % n goes from 0 to 10000
aux=aux+(1/(k^2)); % ∑ (1/n^2)
end
pi2=sqrt(6*aux)
pi2 =
3.1415
Hope it helps.
  3 comentarios
Jan
Jan el 21 de Mzo. de 2013
It surprises me frequently, that so many beginners include a "clear all, clc" in their code. It seems like your professor has not only "done very little to teach us anything about how to use matlab", but the few things he told you, are inefficient wasters of time also. "clear all" removes, beside other things, all loaded function from the memory. Reloading them from disk and parsing them is very time-consuming in Matlab, becaus ethe harddisk access and the code-analysis are expensive. Nevertheless, the brute clean up seems to be a fashion, and instructors teach this, because they have been taught this already. But the opinion of the majority is no necessarily useful of efficient.
There is no limit of the minus operation. Matlab does not get tired or something like that. The problem is, that double values can store integers only up to a range to 2^52 exactly, because there is only space for about 16 digits in the 8 bytes used to store a double. Inside a loop you can reduce the chance to collide with this limitation. So I strongly suggest to try a FOR loop instead, although it might be confusing at first.
Mark
Mark el 21 de Mzo. de 2013
I guess i misstated my last comment, and I found what I need to do, although I havent found a solution. It is impossible to subtract 2 different sized matrices. I am looking to find a way to pad the smaller matrix with a zero to the two can matrices can subtract from one another. Thank you for the advice in regards to the clc and clear all functions. I feel like I am running around in the dark. The only students in our class that are getting this are people that have previously been exposed to MATLAB. I have one last shot at this. If it doesn't work then I will make a loop

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by