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

45 views (last 30 days)
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!

Answers (2)

Wayne King
Wayne King on 14 Mar 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 on 14 Mar 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 Comments
Mark
Mark on 21 Mar 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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!