Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Matlab problem

Subject: Matlab problem

From: Maite

Date: 6 Oct, 2010 12:24:21

Message: 1 of 13

Hello everyone,
We, Maite and Valentina, have math in Uni in the Netherlands and we have to deal with Matlab. However, we are stuck at this problem, not knowing how to solve it. We hope that one of you can help us with this problem:

Write a Matlab programme which helps you to find the first value of N (start at N=1) for
which the sum of the eigenvalues of the Magic matrix of order N exceeds 1000.

Thanks in advance,
desperate students,
Maite and Valentina

Subject: Matlab problem

From: Sean

Date: 6 Oct, 2010 13:21:05

Message: 2 of 13

Maite <maitelameris@hotmail.com> wrote in message <2031347303.58919.1286367891254.JavaMail.root@gallium.mathforum.org>...
> Hello everyone,
> We, Maite and Valentina, have math in Uni in the Netherlands and we have to deal with Matlab. However, we are stuck at this problem, not knowing how to solve it. We hope that one of you can help us with this problem:
>
> Write a Matlab programme which helps you to find the first value of N (start at N=1) for
> which the sum of the eigenvalues of the Magic matrix of order N exceeds 1000.
>
> Thanks in advance,
> desperate students,
> Maite and Valentina

You have to deal with MATLAB? Wow that sounds harsh, I think TMW might have a therapy toolbox in the making.

So what's hard about your problem, what part don't you understand? The while loop, the magic square, the eigenvalues, sum? Show us what you've done and where it isn't working.

Your whole program can be written in three lines so that should be a minimal amount of "dealing with".

Subject: Matlab problem

From: Maite

Date: 8 Oct, 2010 11:15:34

Message: 3 of 13

First of all, thanks for the reply. You're right about matlab, it's very difficult if you've never worked with it before.
This is what we've got until now:

N=1
A=magic(N)
while sum(eig(A))<1000
N=N+1
end
N

We weren't able to check if its working in Matlab, because there was a maintenance problem at uni with the computers. We'd love to hear what you think!
Thanks in advance,
Maite and Valentina

Subject: Matlab problem

From: Sean

Date: 8 Oct, 2010 12:25:05

Message: 4 of 13

> This is what we've got until now:
>
> N=1
> A=magic(N)
> while sum(eig(A))<1000
> N=N+1
> end
> N
>
> We weren't able to check if its working in Matlab, because there was a maintenance problem at uni with the computers. We'd love to hear what you think!

You're general idea is right; however, there are a two issues with your order:
1) When N = 64636 (as it ran to on my computer before I manually stopped it), how big is A? Though this is easier to identify with an active MATLAB session in front of you it should be obvious to you.
Once you figure that out:
2) Let's say *hypothetically* the sum of the magic square's eigenvalues exceeds 1000 when N = 13. What is your program going to output for N? I'll give you a hint: not 13. This is an exercise in initiating and using a counter, you're doing it the wrong way for this goal, there are times when the way you are doing it is correct.

Subject: Matlab problem

From: Maite

Date: 8 Oct, 2010 15:45:13

Message: 5 of 13

Thanks for the reply, however, we still have some difficulties:

1) When N=64636, A would be a very big matrix of order 64636. But we can’t figure out how it came to such a high N-value. Using a While-statement should make the program stop when this condition (sum(eig(A))<1000) doesn’t hold anymore, shouldn’t it? That couldn’t be for such a high N, could it?

2) We want to know which order the matrix has (N) when the sum of the eigenvalues is for the first time over a 1000. With initiating a counter, do you mean like this:

Count=0
N=1
A=magic(N)
While sum(eig(A))<1000
N=N+1
Count=Count+1
End
N
Count

This is another program, we thought about:
N=1
A=magic(N)
If sum(eig(A))>=1000
N
Else N=N+1
End

Thank you for the help you’ve already given us, and we hope you would look at the program again. The computers at uni aren’t available, because of the problems. But we will try the program on Monday (no other possibilities
:(), thats why we still have difficulties seeing the issues with our program.
Well, thank you!
Maite and Valentina

Subject: Matlab problem

From: Torsten Hennig

Date: 8 Oct, 2010 15:58:33

Message: 6 of 13

> Thanks for the reply, however, we still have some
> difficulties:
>
> 1) When N=64636, A would be a very big matrix of
> order 64636. But we can’t figure out how it came to
> such a high N-value. Using a While-statement should
> make the program stop when this condition
> (sum(eig(A))<1000) doesn’t hold anymore, shouldn’t
> it? That couldn’t be for such a high N, could it?
>
> 2) We want to know which order the matrix has (N)
> when the sum of the eigenvalues is for the first time
> over a 1000. With initiating a counter, do you mean
> like this:
>
> Count=0
> N=1
> A=magic(N)
> While sum(eig(A))<1000
> N=N+1
> Count=Count+1
> End
> N
> Count
>
> This is another program, we thought about:
> N=1
> A=magic(N)
> If sum(eig(A))>=1000
> N
> Else N=N+1
> End
>
> Thank you for the help you’ve already given us, and
> we hope you would look at the program again. The
> computers at uni aren’t available, because of the
> problems. But we will try the program on Monday (no
> other possibilities
> :(), thats why we still have difficulties seeing the
> issues with our program.
> Well, thank you!
> Maite and Valentina

As Sean already alluded to with his first remark,
the size of your matrix A remains (1x1) for both
programming versions above since it is generated
once at the beginning _outside_ the while-loop.

Best wishes
Torsten.

Subject: Matlab problem

From: Sean

Date: 8 Oct, 2010 16:11:04

Message: 7 of 13

Torsten Hennig <Torsten.Hennig@umsicht.fhg.de> wrote in message <875372480.72537.1286553543546.JavaMail.root@gallium.mathforum.org>...
> > Thanks for the reply, however, we still have some
> > difficulties:
> >
> > 1) When N=64636, A would be a very big matrix of
> > order 64636. But we can’t figure out how it came to
> > such a high N-value. Using a While-statement should
> > make the program stop when this condition
> > (sum(eig(A))<1000) doesn’t hold anymore, shouldn’t
> > it? That couldn’t be for such a high N, could it?
> >
> > 2) We want to know which order the matrix has (N)
> > when the sum of the eigenvalues is for the first time
> > over a 1000. With initiating a counter, do you mean
> > like this:
> >
> > Count=0
> > N=1
> > A=magic(N)
> > While sum(eig(A))<1000
> > N=N+1
> > Count=Count+1
> > End
> > N
> > Count
> >
> > This is another program, we thought about:
> > N=1
> > A=magic(N)
> > If sum(eig(A))>=1000
> > N
> > Else N=N+1
> > End
> >
> > Thank you for the help you’ve already given us, and
> > we hope you would look at the program again. The
> > computers at uni aren’t available, because of the
> > problems. But we will try the program on Monday (no
> > other possibilities
> > :(), thats why we still have difficulties seeing the
> > issues with our program.
> > Well, thank you!
> > Maite and Valentina
>
> As Sean already alluded to with his first remark,
> the size of your matrix A remains (1x1) for both
> programming versions above since it is generated
> once at the beginning _outside_ the while-loop.
>
> Best wishes
> Torsten.

Exactly. When is the magic square ever going to be of order 64636? My laptop doesn't have enough RAM to make that.

Subject: Matlab problem

From: Steven_Lord

Date: 8 Oct, 2010 17:09:42

Message: 8 of 13



"Maite" <maitelameris@hotmail.com> wrote in message
news:1302467408.72443.1286552744352.JavaMail.root@gallium.mathforum.org...
> Thanks for the reply, however, we still have some difficulties:
>
> 1) When N=64636, A would be a very big matrix of order 64636.

The question was not what _would_ A be but what _is_ A. I think you'll find
that the way you've written your code in fact A is NOT a 64636-by-64636
matrix when N is equal to 64636. Think about how to correct that problem.

> But we cant figure out how it came to such a high N-value. Using a
> While-statement should make the program stop when this condition
> (sum(eig(A))<1000) doesnt hold anymore, shouldnt it? That couldnt be
> for such a high N, could it?

Why not? What are the eigenvalues of -eye(N)? What is the sum of those
eigenvalues as a function of N?

> 2) We want to know which order the matrix has (N) when the sum of the
> eigenvalues is for the first time over a 1000. With initiating a counter,
> do you mean like this:
>
> Count=0
> N=1
> A=magic(N)
> While sum(eig(A))<1000
> N=N+1
> Count=Count+1
> End
> N
> Count
>
> This is another program, we thought about:
> N=1
> A=magic(N)
> If sum(eig(A))>=1000
> N
> Else N=N+1
> End
>
> Thank you for the help youve already given us, and we hope you would look
> at the program again. The computers at uni arent available, because of
> the problems. But we will try the program on Monday (no other
> possibilities
> :(), thats why we still have difficulties seeing the issues with our
> program.

In this case, work through your code, line by line, using pencil and paper
(or marker and whiteboard, or your favorite writing utensil/writing material
combination.) Execute the code exactly as you're written it (not as you
think it should work.) I think you'll locate the problem fairly quickly,
after a few iterations through the loop.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Matlab problem

From: Maite

Date: 8 Oct, 2010 17:17:32

Message: 9 of 13

Aha, probabaly very stupid of us, but that is something what I call a beginners mistake..Haha.
Thanks!
But you should specify a start of N at the beginning right? If you say N=1 in the While-loop, wouldn't you have the same problem? when running N from 1 till the condition holds, should we specify that N runs from 1 till infinity? Or at least make it running in stead of staying at 1. Like this?

N=[1:]
A=magic(N)
While sum(eig(A))<1000
N=N+1
End
N

If I remember correctly, running like N=[1:] will make sure N runs from 1 on. N=[1:inf] will be a command that generates an error, I think. I deleted the count, because I think its unnecessary, because we're looking for a value for N.
Thanks for you replies!
Maite and Valentina

Subject: Matlab problem

From: Sean

Date: 8 Oct, 2010 17:35:04

Message: 10 of 13


> N=[1:]
> A=magic(N)
> While sum(eig(A))<1000
> N=N+1
> End
> N
>
> If I remember correctly, running like N=[1:] will make sure N runs from 1 on. N=[1:inf] will be a command that generates an error, I think. I deleted the count, because I think its unnecessary, because we're looking for a value for N.

No!!! You're still missing the point. You never update A. You update N, but that doesn't mean anything, A will always be 1x1. What you need is to redefine A in the while loop with the current size of N.

Subject: Matlab problem

From: Maite

Date: 8 Oct, 2010 18:08:35

Message: 11 of 13

Oh my gosh, I'm sooo confused right now. I thought the order of the matrix would change with N. I hope you will have one more go with the new updated version, if its still nothing, then thanks for all the help. I guess I'm too much of a layman for this and I do not want to frustate everybody (as I am right now!)

S=1
While A=magic(N)
sum(eig(A))<1000
N=S+1
End
N

I know I keep saying it, but thanks again!
And I hope this is better than the last one and you still have the energy to look at it. Either way, thanks!
King regards,
Maite

Subject: Matlab problem

From: Sean

Date: 8 Oct, 2010 18:27:03

Message: 12 of 13

Maite <maitelameris@hotmail.com> wrote in message <969165795.73194.1286561345515.JavaMail.root@gallium.mathforum.org>...
> Oh my gosh, I'm sooo confused right now. I thought the order of the matrix would change with N. I hope you will have one more go with the new updated version, if its still nothing, then thanks for all the help. I guess I'm too much of a layman for this and I do not want to frustate everybody (as I am right now!)
>
> S=1
> While A=magic(N)
> sum(eig(A))<1000
> N=S+1
> End
> N
>
> I know I keep saying it, but thanks again!
> And I hope this is better than the last one and you still have the energy to look at it. Either way, thanks!
> King regards,
> Maite


Nope. The entrance to your while-loop needs to be a logical scalar value. Is assigning A to be magic(N) ever going to be a logical scalar value?

Subject: Matlab problem

From: Maite

Date: 8 Oct, 2010 18:49:31

Message: 13 of 13

Aaaahhh, well I give up. Thanks a lot for your help!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us