Matlab - floating point operation counts (flop count)

In summary, the conversation was about a problem involving calculating the number of flops in a given sequence of commands. The discussion delved into understanding nested loops and how to write them in different programming languages. The conversation also touched on finding the value of a using summation formulas. The conversation concluded with a summary of the pattern observed in the values of a for different values of n. It was ultimately understood that the expression n^2 + (n-1)^2 + (n-2)^2 + ... + 1^2 can be used to calculate the value of a for any given value of n.
  • #1
playboy

Homework Statement



So I was giving a problem to "calculate" the number of flops in class today..
Now, I read the examples in the textbook, and they are hard enough to understand...I am hoping that some one here can push me onto the right track :cool:



Homework Equations



find the flop count for the following sequence of commands:
a=0;
..for p=1:n
...for q=p:n
...for r=q:n
...a=a+1;
...end
...end
..end
What will be the value of a after the commands are executed ?

The Attempt at a Solution



I am not looking for a number, but a summation formula interms of n.

I am given that it has to be in the form
sum(i,n) i = n(1-n)/2
and
sum(i,n) i^2 = n(1-n)(2n-a)/6

So far, all I can see is that their must be n + n + n = 3n flops...

I have no idea how to calculate a.

Can somebody help me please?
 
Physics news on Phys.org
  • #2
So far, all I can see is that their must be n + n + n = 3n flops...

That's not correct for two reasons:
1 The loops are nested inside each other
2 The loops do not all go from 1 to n.

It might help to change this into a real computer language so you can run the program, set n equal to a small number like 4 or 5, and print out p, q, r, and a in the inside loop (just after the a = a + 1) . Then you will see what's going on.
 
  • #3
AlephZero said:
That's not correct for two reasons:
1 The loops are nested inside each other
2 The loops do not all go from 1 to n.

It might help to change this into a real computer language so you can run the program, set n equal to a small number like 4 or 5, and print out p, q, r, and a in the inside loop (just after the a = a + 1) . Then you will see what's going on.

Perhaps that is my problem..."the loops are nested inside each other"

I tried getting this programme to run so that I can "visualize" how it works, but I can't get MATLAB running it.

I wrote:

function [a]=bs(n)

a=0;
for p=1:n;
for q=1:n;
for r=1:n;
a=a+1;
end
end
end


and tried running [a]=bs(n) for n=3 in the commad window. I got an error.

Let's consider n = 5

p=1:5 --> p = 1 2 3 4 5
q=1:5 --> q = 1 2 3 4 5
r=1:5 --> r = 1 2 3 4 5

So i have NO idea how this loop works :eek:
 
  • #4
You need to find out what a "for" loop means, in the language that is being used in these questions.

The code

for p = 1:5
(some statements)
end

doesn't mean the same as the Matlab statement

p = 1:5
 
  • #5
I think my problem is understanding the entire code.

I've been pouring my thoughts out for the longest time on this question.

Could you please expalin to me how the function would compute a for n=2?

(I tried writing this function in MATLAB but failed)
 
  • #6
I'll repeat what I said before: if you don't understand the concept of loops in procedural programming languages, then you need to learn about that, before you can do this type of question.

If you knew a language like C, C++, Java, Pascal, Basic, Fortran, etc, then it should be obvious what the code means, even though the notation is not exactly the same as any of those languages.

If you haven't done any courses on programming, there is a tutorial on C++ here: https://www.physicsforums.com/showthread.php?t=32703. Your "for p=1:n" means the same "for (p = 1; p <= n; p++)" in C or C++.
 
  • #7
a=0;
..for p=1:n
...for q=p:n
...for r=q:n
...a=a+1;
...end
...end
..end
For n= 2.
First a is set equal to 0.
Now, p is set to 1, q is set to p= 1, r is set to q= 1
a is increased to 1

We are still in the "r" loop- now r is set to 2
a is increased to 2

Since r has reached its maximum we drop back to the q loop
q is set to 2
r is set to q= 2
a is increased to 3

Since r has reached its maximum we drop back to the q loop
Since q has reached its maximum we drop back to the p koop
p is set to 2, q is set to p= 2, r is set to q= 2
a is increased to 4

Since r has reached its maximum we drop back to the q loop
Since q has reached its maximum we drop back to the p loop
Since p has reached its maximum the program terminates.

a is equal to 4= 22 but be careful. Try n= 3 before making any hasty generaliztions!
Once you find a relation that you feel sure of, perhaps you can prove by induction on n.
 
Last edited by a moderator:
  • #8
Thank you SOO much Hallsofivy! I really appreciate how you took the time to explain that to me..it reall helped me.

So I did this up until n=7 :zzz:

n=1 a=1 ----> n^2 + 0 = a
n=2 a=4 ----> n^2 + 0 = a
n=3 a=10 ---> n^2 + 1 = a
n=4 a=20 ---> n^2 + 4 = a
n=5 a=35 ---> n^2 + 10 = a
n=6 a=56 ---> n^2 + 20= a
n=7 a=84 ---> n^2 + 35 = a

I notice the pattern and I can see what has to be done.

I really appreciate your help. I know sincerely understand what is going on.
Thank you HallsofIvy!
 

1. What is a "floating point operation count" in Matlab?

A floating point operation count (flop count) in Matlab refers to the number of arithmetic operations performed by a computer program using floating point numbers. These operations include addition, subtraction, multiplication, and division.

2. Why is it important to track the flop count in Matlab?

Tracking the flop count in Matlab is important because it allows scientists and programmers to analyze the efficiency and complexity of their algorithms. By knowing the number of floating point operations, they can optimize their code and improve the performance of their programs.

3. How does Matlab calculate the flop count?

Matlab calculates the flop count by analyzing the code and counting the number of arithmetic operations performed on floating point numbers. It also takes into account the number of iterations and function calls.

4. Can the flop count vary for the same code in Matlab?

Yes, the flop count can vary for the same code in Matlab due to different factors such as the input data, the hardware and software used, and the optimization settings. Different implementations of the same algorithm can also result in different flop counts.

5. How can I reduce the flop count in Matlab?

To reduce the flop count in Matlab, you can optimize your code by using efficient algorithms and data structures, minimizing the number of operations, and avoiding unnecessary calculations. You can also take advantage of Matlab's built-in functions and vectorization techniques to decrease the number of floating point operations.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
738
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Linear and Abstract Algebra
Replies
2
Views
979
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top