My Math Forum  

Go Back   My Math Forum > Math Forums > Math Software

Math Software Math Software - Mathematica, Matlab, Calculators, Graphing Software


Thanks Tree3Thanks
  • 3 Post By skipjack
Reply
 
LinkBack Thread Tools Display Modes
February 10th, 2016, 09:24 AM   #1
Newbie
 
Joined: Feb 2016
From: Pretoria

Posts: 3
Thanks: 0

Octave crisis

Hi, I am using a software called octave to do calculations. Using the software, I am supposed to calculate the sum of the series: 1^2 +2^2 +3^2 +4^2...100^2. Please help!
Johnnyesto is offline  
 
February 10th, 2016, 09:37 AM   #2
Math Team
 
Joined: Jan 2015
From: Alabama

Posts: 2,647
Thanks: 680

What help do you want? Someone to tell you how to use the software? Do you have a manual for it?
Country Boy is offline  
February 10th, 2016, 11:10 AM   #3
Newbie
 
Joined: Feb 2016
From: Pretoria

Posts: 3
Thanks: 0

I do have a manual for it, but it is no good. I wish I knew where to study.
Johnnyesto is offline  
February 10th, 2016, 11:44 AM   #4
Global Moderator
 
Joined: Dec 2006

Posts: 17,919
Thanks: 1383

You could do this for any function of the integers.

If you define a function g, then sum(g([1:100])) gives the sum of its first 100 values.
Thanks from ZardoZ, Country Boy and OmniBear
skipjack is offline  
February 11th, 2016, 05:07 AM   #5
Newbie
 
Joined: Feb 2016
From: Pretoria

Posts: 3
Thanks: 0

Thank you skipjack. I truly appreciate your ingenuity.
Johnnyesto is offline  
February 29th, 2016, 05:21 AM   #6
Math Team
 
Joined: Nov 2010
From: Greece, Thessaloniki

Posts: 1,989
Thanks: 133

Math Focus: pre pre pre pre pre pre pre pre pre pre pre pre calculus
simple for loop

Skipjack's way of computing is much faster.

Here is a lightly heavier way (takes slightly much more time for the program to search in the libraries for - loop). Open with a notepad an empty file, name the file sqr_sum.m and then paste inside the following:
%----------------------------------------------------------------------
fprintf('This simple for-loop calculates the sum : 1 + 2^2 + ... + n^2\n');
n = input('Enter the natural number n: ');
if n<=0
fprintf('This is an invalid input! Sum will be computed for n = 1.\n')
n = 1;
end
sum = 0;
fprintf('Calculating ...');
for ii = 1 : n
sum = sum + ii^2;
end
fprintf('The sum is: %i \n',sum);
%----------------------------------------------------------------------

Navigate octave to the folder that you saved your file and type:
>sqr_sum

Last edited by ZardoZ; February 29th, 2016 at 05:29 AM.
ZardoZ is offline  
February 29th, 2016, 06:28 AM   #7
Senior Member
 
Joined: Apr 2014
From: Glasgow

Posts: 2,049
Thanks: 680

Math Focus: Physics, mathematical modelling, numerical and computational solutions
The current answers are great.

However, if it's the software writing part you find difficult (rather than the maths bit), consider a technique called 'writing pseudocode'. Pseudocode is a nice technique to help figure out what a program should look like... you just write out a set of instructions, step-by-step, about what sort of things you want the program to do. Start with a simple recipe-like set of instructions and then go into more and more detail bit by bit.


So, to begin with you'll probably have some really basic steps, like this

1. Input some numbers in
2. Calculate the series
3. Print the answers

Okay... let's go into more detail... firstly, we don't have any numbers to input, so we can get rid of step 1. Secondly, a series has lots of terms in it, so we want to add up all those terms together to make a sum. So instead we might instead have something like this:

1. set the current sum to be 0
2. work out what the next term is
3. add this term to the current sum
4. repeat steps 3-4 until you've gone over all the terms
5. Print current sum to the screen


Okay... let's try going into even more detail. Each term is calculated by squaring a number. The number to square goes up by 1 each time too:

1. set current sum = 0
2. set current number = 1
3. new term = current number squared
4. current sum = current sum + new term
5. current number = current number +1
6. repeat steps 3-5 until current_number is 100
7. print current sum

Steps 4 and 5 might look odd... how can current sum = current sum + new term? Basically, computers see equals signs (=) differently than how they are used in maths. A computer sees an equals sign as 'assign', so the computer takes the number (like current sum) and replaces it with a new one which is slightly bigger (current sum + new term). Therefore, as the program goes over the steps, the number will get bigger and bigger.

Now you can look up in the technical manual for the precise language (also known as syntax) you are using and you can write down the various things above into an actual computer program. Some tips:

- Whenever you have to repeat things, look up loops.
- Whenever you need to repeat some complicated mathematics over and over again, look up functions.
- If there's things that should only work under precise conditions, look up conditionals

Here's some code that might work:

Code:
currentSum = 0;

for currentNum = 1:100
    currentSum = currentSum + n^2;
end

fprintf('The sum is: %i \n',currentSum);
This is actually similar to ZardoZ's result, only that result is better because you can input the sum you want to calculate, so if you wanted to sum to a different number than 100, you can

In any case, I hope this helps!
Benit13 is offline  
Reply

  My Math Forum > Math Forums > Math Software

Tags
crisis, help!, octave



Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Identity. Crisis. yogazen2013 Algebra 3 September 6th, 2013 10:50 AM
Finding the coefficients of a polynomial - Octave biket Computer Science 1 January 20th, 2013 09:27 AM
How to solve * Plot a primitive of sin(x^3). in octave? MisterT Calculus 3 December 28th, 2010 12:18 AM
Finding the coefficients of a polynomial - Octave biket Algebra 0 December 31st, 1969 04:00 PM





Copyright © 2017 My Math Forum. All rights reserved.