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

  • Context: MATLAB 
  • Thread starter Thread starter eurekameh
  • Start date Start date
  • Tags Tags
    Matlab Summation
Click For Summary

Discussion Overview

The discussion revolves around how to perform the summation of \(x^2\) from 0 to 3 in MATLAB without using built-in functions. Participants explore coding techniques, particularly focusing on the use of loops and vectorized approaches.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant seeks guidance on implementing a summation using a for loop, expressing difficulty in storing and adding values.
  • Another participant suggests showing the best attempt at the code to facilitate further assistance.
  • A participant proposes initializing a variable to store the sum and using a loop to calculate each power of \(x\) and add it to the sum.
  • There is mention of creating a vector of ones to facilitate a vectorized approach, while avoiding built-in functions.
  • Another participant notes that the sum of squares can be achieved through the dot product of a vector with itself, suggesting a vector-based method.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach, as multiple methods are proposed, including both loop-based and vectorized strategies. The discussion remains open with various suggestions and no definitive resolution.

Contextual Notes

Some participants express uncertainty about the use of certain MATLAB functions and the implications of avoiding built-in functions. There is also a lack of clarity on the specifics of the implementation details for the proposed methods.

Who May Find This Useful

This discussion may be useful for individuals learning MATLAB programming, particularly those interested in coding without relying on built-in functions, as well as those exploring different approaches to summation and vector operations.

eurekameh
Messages
209
Reaction score
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
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.
 
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.
 
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)
 
... 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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K