Summation x^2 0-3 w/o Built-in Matlab Fns: For Loop

In summary: The sum of the squares of the elements of a vector is just the dot product with itself right? So - set up a vector of sequential numbers and dot product it with itself:x=1:9;s=x*x';and s is the sum you want.
  • #1
eurekameh
210
0
How can I do the summation of x^2 from 0 to 3 without the use of any built-in functions? I know a for loop is involved, but I can't get it to work.
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Sounds like an assignment ... the idea is for you to figure it out for yourself.
However: doesn't mean we cannot give you a nudge in the right direction ;)

Show us your best attempt and what happens when you run it.
 
  • #3
Lol, it's not an assignment per se, but I still need this piece of code.
I tried:

i = 0:1:3
x = i.^2
end

but this just uses each i = 1, i = 2, i = 3 to compute x = i^2. I'm looking for a way to somehow store each xi and add them to form the total summation.
 
  • #4
eurekameh said:
Lol, it's not an assignment per se, but I still need this piece of code.
I tried:

i = 0:1:3
x = i.^2
end

but this just uses each i = 1, i = 2, i = 3 to compute x = i^2. I'm looking for a way to somehow store each xi and add them to form the total summation.
I'm not a Matlab user, but does x already store each i^2? There are a couple of ways you could do this: using a loop or a vectorized approach.

With a loop, look up 'for' and 'while' - essentially, you initialize the variable that you want to store the sum, then within a loop calculate each power and add it to the sum variable.

Alternatively, create a vector of '1's the same length as x and multiply them together vectorwise. ... the built-in function 'ones' is the obvious route to go but as you don't want to use built-in functions, do something like
o = x*0+1 (multiply x by 0 to create a zero vector of same size as x, then add 1)
s = x*o (scalar product of o and x)
 
  • #5
... and then there is always looking up the sum.m file that houses the built-in function that MATLAB uses ;)

Note: the sum of the squares of the elements of a vector is just the dot product with itself right? So - set up a vector of sequential numbers and dot product it with itself:

x=1:9;
s=x*x';

and s is the sum you want.

Matlab really rewards vector-based thinking.
 

Related to Summation x^2 0-3 w/o Built-in Matlab Fns: For Loop

1. What is "Summation x^2 0-3 w/o Built-in Matlab Fns: For Loop"?

"Summation x^2 0-3 w/o Built-in Matlab Fns: For Loop" is a mathematical concept that involves calculating the sum of the squares of a series of numbers from 0 to 3 without using any built-in functions in the Matlab software.

2. Why would someone want to calculate this summation without using built-in Matlab functions?

Calculating this summation without using built-in functions can help improve a scientist's understanding of the underlying mathematical concepts and can also be useful in situations where built-in functions may not be available or appropriate.

3. How can I perform this summation using a for loop in Matlab?

To perform this summation using a for loop in Matlab, you can use a loop counter to iterate through the series of numbers and use the loop counter variable to calculate and store the sum of the squared values.

4. Can this summation be performed using other programming languages?

Yes, this summation can be performed using for loops or other iterative methods in other programming languages such as Python, Java, or C++. The specific syntax may vary, but the underlying concept remains the same.

5. Are there any advantages to using this method over built-in Matlab functions?

Using this method can be advantageous for educational purposes or in situations where built-in functions may not be available. It can also be useful in scenarios where the series of numbers is not a continuous range and may require additional manipulation before using built-in functions.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
716
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Back
Top