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:
out of memory while using roots

Subject: out of memory while using roots

From: Snow White

Date: 11 May, 2010 13:02:05

Message: 1 of 14

Hello,

I get the following problem in my code can someone help me out with this. Thank you.

A=((sin(temp_theeta_incident).^2).*(1+temp_rho)-2.*temp_rho/(4.*temp_rho).*cos(temp_theeta_incident));
B=((sin(temp_theeta_incident).^2).*(1+temp_rho))/(4.*temp_rho.*cos(temp_theeta_incident));
C=(-2.*(sin(temp_theeta_incident).^2).*(1-temp_rho)+temp_rho)./(4.*temp_rho.*cos(temp_theeta_incident));

 p = [A.^2 2*A.*C-1 2*A.*B+C.^2+D 2*B.*C B.^2];
            r= roots(p);

For the above code i get the following error:

??? Error using ==> diag
Out of memory. Type HELP MEMORY for your options.

Error in ==> roots at 61
    a = diag(ones(1,n-2,class(c)),-1);

Error in ==> root_calculation at 26
            r= roots(p); %%computing roots of the equation inorder to find the refractive index

size(temp_rho) is 1 105600

Bye

Subject: out of memory while using roots

From: Steven Lord

Date: 11 May, 2010 13:08:45

Message: 2 of 14


"Snow White" <gulesaman@gmail.com> wrote in message
news:hsbkgd$7pu$1@fred.mathworks.com...
> Hello,
>
> I get the following problem in my code can someone help me out with this.
> Thank you.
>
> A=((sin(temp_theeta_incident).^2).*(1+temp_rho)-2.*temp_rho/(4.*temp_rho).*cos(temp_theeta_incident));
> B=((sin(temp_theeta_incident).^2).*(1+temp_rho))/(4.*temp_rho.*cos(temp_theeta_incident));
> C=(-2.*(sin(temp_theeta_incident).^2).*(1-temp_rho)+temp_rho)./(4.*temp_rho.*cos(temp_theeta_incident));
>
> p = [A.^2 2*A.*C-1 2*A.*B+C.^2+D 2*B.*C B.^2];
> r= roots(p);
>
> For the above code i get the following error:
>
> ??? Error using ==> diag
> Out of memory. Type HELP MEMORY for your options.

Assuming that D is a scalar (1-by-1) and that temp_theeta_incident is the
same size as temp_rho, this means you're trying to find the roots of a
polynomial of degree greater than _4 MILLION_.

Is that truly your intention? I'm guessing it is not. If it is not, you
need to explain what specifically you're trying to do so we can attempt to
determine how to do it without performing this ROOTS calculation.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: out of memory while using roots

From: Snow White

Date: 11 May, 2010 13:24:04

Message: 3 of 14

D=(sin(temp_theeta_incident)^2);
size(temp_rho) is 1 105600

theoretically i should get 4 roots for each matrix element (image pixel). is there a solution for this?:s

"Steven Lord" <slord@mathworks.com> wrote in message <hsbksl$2dc$1@fred.mathworks.com>...
>
> "Snow White" <gulesaman@gmail.com> wrote in message
> news:hsbkgd$7pu$1@fred.mathworks.com...
> > Hello,
> >
> > I get the following problem in my code can someone help me out with this.
> > Thank you.
> >
> > A=((sin(temp_theeta_incident).^2).*(1+temp_rho)-2.*temp_rho/(4.*temp_rho).*cos(temp_theeta_incident));
> > B=((sin(temp_theeta_incident).^2).*(1+temp_rho))/(4.*temp_rho.*cos(temp_theeta_incident));
> > C=(-2.*(sin(temp_theeta_incident).^2).*(1-temp_rho)+temp_rho)./(4.*temp_rho.*cos(temp_theeta_incident));
> >
> > p = [A.^2 2*A.*C-1 2*A.*B+C.^2+D 2*B.*C B.^2];
> > r= roots(p);
> >
> > For the above code i get the following error:
> >
> > ??? Error using ==> diag
> > Out of memory. Type HELP MEMORY for your options.
>
> Assuming that D is a scalar (1-by-1) and that temp_theeta_incident is the
> same size as temp_rho, this means you're trying to find the roots of a
> polynomial of degree greater than _4 MILLION_.
>
> Is that truly your intention? I'm guessing it is not. If it is not, you
> need to explain what specifically you're trying to do so we can attempt to
> determine how to do it without performing this ROOTS calculation.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

Subject: out of memory while using roots

From: Steven Lord

Date: 11 May, 2010 14:13:22

Message: 4 of 14


"Snow White" <gulesaman@gmail.com> wrote in message
news:hsblpk$p0$1@fred.mathworks.com...
> D=(sin(temp_theeta_incident)^2);
> size(temp_rho) is 1 105600
>
> theoretically i should get 4 roots for each matrix element (image pixel).
> is there a solution for this?:s

Create a 4-by-105600 matrix theroots, then loop from k = 1 to 105600.
Create p from the kth elements of A, B, C, and D and compute the roots of
that polynomial, then store them in the corresponding column of theroots.

Alternately you _could_ use some of the methods described in section 3 of
this Wikipedia page:

http://en.wikipedia.org/wiki/Quartic_function

and simply plug in your vectors to one or more of those expressions.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: out of memory while using roots

From: Snow White

Date: 11 May, 2010 23:33:20

Message: 5 of 14

Hello,

are you saying i shud compute all the values in a loop? A B etc isnt that going to take a long time.

Bye

"Steven Lord" <slord@mathworks.com> wrote in message <hsbolq$7vj$1@fred.mathworks.com>...
>
> "Snow White" <gulesaman@gmail.com> wrote in message
> news:hsblpk$p0$1@fred.mathworks.com...
> > D=(sin(temp_theeta_incident)^2);
> > size(temp_rho) is 1 105600
> >
> > theoretically i should get 4 roots for each matrix element (image pixel).
> > is there a solution for this?:s
>
> Create a 4-by-105600 matrix theroots, then loop from k = 1 to 105600.
> Create p from the kth elements of A, B, C, and D and compute the roots of
> that polynomial, then store them in the corresponding column of theroots.
>
> Alternately you _could_ use some of the methods described in section 3 of
> this Wikipedia page:
>
> http://en.wikipedia.org/wiki/Quartic_function
>
> and simply plug in your vectors to one or more of those expressions.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

Subject: out of memory while using roots

From: Snow White

Date: 12 May, 2010 11:07:03

Message: 6 of 14

I tried the following:

for Nc_index=1:Nc
                theroots(:,Nc_index)= roots(p(Nc_index)); %%computing roots of the equation inorder to find the refractive index
            end
But when i chk the size :
size(theroots) is 0 105600
 although i have already defined theroots

bye

"Snow White" <gulesaman@gmail.com> wrote in message <hscpg0$5qc$1@fred.mathworks.com>...
> Hello,
>
> are you saying i shud compute all the values in a loop? A B etc isnt that going to take a long time.
>
> Bye
>
> "Steven Lord" <slord@mathworks.com> wrote in message <hsbolq$7vj$1@fred.mathworks.com>...
> >
> > "Snow White" <gulesaman@gmail.com> wrote in message
> > news:hsblpk$p0$1@fred.mathworks.com...
> > > D=(sin(temp_theeta_incident)^2);
> > > size(temp_rho) is 1 105600
> > >
> > > theoretically i should get 4 roots for each matrix element (image pixel).
> > > is there a solution for this?:s
> >
> > Create a 4-by-105600 matrix theroots, then loop from k = 1 to 105600.
> > Create p from the kth elements of A, B, C, and D and compute the roots of
> > that polynomial, then store them in the corresponding column of theroots.
> >
> > Alternately you _could_ use some of the methods described in section 3 of
> > this Wikipedia page:
> >
> > http://en.wikipedia.org/wiki/Quartic_function
> >
> > and simply plug in your vectors to one or more of those expressions.
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> >

Subject: out of memory while using roots

From: Steven Lord

Date: 12 May, 2010 14:05:25

Message: 7 of 14


"Snow White" <gulesaman@gmail.com> wrote in message
news:hscpg0$5qc$1@fred.mathworks.com...
> Hello,
>
> are you saying i shud compute all the values in a loop? A B etc isnt that
> going to take a long time.

No, you can compute A, B, C, and D as you did before (and if you made them
column vectors instead of row vectors you could even compute your
polynomials all at once) -- but call ROOTS inside the loop.

And as for taking a long time -- you're trying to find the roots of a
hundred thousand polynomials. That's going to take a little bit of time no
matter what you do.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: out of memory while using roots

From: Snow White

Date: 10 Jun, 2010 12:56:04

Message: 8 of 14

Hello,

I tried doing the following:

theroots=zeros(4,Nc);
p= [A.^2 (2.*A.*C-1) ((2.*A.*B)+(C.^2)+D) 2.*B.*C B.^2];
            for Nc_index=1:Nc
               theroots(Nc_index)= roots(p(Nc_index)); %%computing roots of the equation inorder to find the refractive index
            end
i get the following error:
??? Subscripted assignment dimension mismatch.

bye

"Steven Lord" <slord@mathworks.com> wrote in message <hsecis$fjr$1@fred.mathworks.com>...
>
> "Snow White" <gulesaman@gmail.com> wrote in message
> news:hscpg0$5qc$1@fred.mathworks.com...
> > Hello,
> >
> > are you saying i shud compute all the values in a loop? A B etc isnt that
> > going to take a long time.
>
> No, you can compute A, B, C, and D as you did before (and if you made them
> column vectors instead of row vectors you could even compute your
> polynomials all at once) -- but call ROOTS inside the loop.
>
> And as for taking a long time -- you're trying to find the roots of a
> hundred thousand polynomials. That's going to take a little bit of time no
> matter what you do.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

Subject: out of memory while using roots

From: Steven Lord

Date: 10 Jun, 2010 16:00:49

Message: 9 of 14


"Snow White" <gulesaman@gmail.com> wrote in message
news:huqnd4$6h1$1@fred.mathworks.com...
> Hello,
>
> I tried doing the following:
>
> theroots=zeros(4,Nc);
> p= [A.^2 (2.*A.*C-1) ((2.*A.*B)+(C.^2)+D) 2.*B.*C B.^2];
> for Nc_index=1:Nc
> theroots(Nc_index)= roots(p(Nc_index)); %%computing roots of
> the equation inorder to find the refractive index
> end
> i get the following error:
> ??? Subscripted assignment dimension mismatch.

You want to extract _rows_ of p, not individual elements, and store the
results of computing the roots of those polynomials in _rows_ of theroots,
not individual elements. For example, p(2, :) extracts the 2nd row, all
columns, from p. Use that to modify the code appropriately.

--
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: out of memory while using roots

From: Snow White

Date: 10 Jun, 2010 16:51:04

Message: 10 of 14

P is a row vector of size 1 7749245

"Steven Lord" <slord@mathworks.com> wrote in message <hur27e$fti$1@fred.mathworks.com>...
>
> "Snow White" <gulesaman@gmail.com> wrote in message
> news:huqnd4$6h1$1@fred.mathworks.com...
> > Hello,
> >
> > I tried doing the following:
> >
> > theroots=zeros(4,Nc);
> > p= [A.^2 (2.*A.*C-1) ((2.*A.*B)+(C.^2)+D) 2.*B.*C B.^2];
> > for Nc_index=1:Nc
> > theroots(Nc_index)= roots(p(Nc_index)); %%computing roots of
> > the equation inorder to find the refractive index
> > end
> > i get the following error:
> > ??? Subscripted assignment dimension mismatch.
>
> You want to extract _rows_ of p, not individual elements, and store the
> results of computing the roots of those polynomials in _rows_ of theroots,
> not individual elements. For example, p(2, :) extracts the 2nd row, all
> columns, from p. Use that to modify the code appropriately.
>
> --
> 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: out of memory while using roots

From: Steven Lord

Date: 10 Jun, 2010 16:54:31

Message: 11 of 14


"Snow White" <gulesaman@gmail.com> wrote in message
news:hur55o$2hq$1@fred.mathworks.com...
>P is a row vector of size 1 7749245

Didn't we have this discussion earlier in the thread? Look at posts 4 and 7
in this thread.

Make A, B, C, and D column vectors.
Combine them together to make an N-by-5 matrix p.
Loop over the rows of p.
    Calculate the ROOTS of each row and store them in the rows of theroots.

--
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: out of memory while using roots

From: Snow White

Date: 10 Jun, 2010 18:04:20

Message: 12 of 14

Thank you.

I have done as u had suggested but i dont understand how should i index the resultant theroots? i have done the following:

[pr,pc]=size(p);
for pr_index=1:pr
theroots(:,:)= roots(p(pr_index,:)); %%computing roots of the equation inorder to find the refractive index
            end
but that ofcourse only gives the last value stored in it. and the size is as follows:
size(theroots(:,:))

ans =

     4 1

i am confused about how to extract the roots at each point.
bye
"Steven Lord" <slord@mathworks.com> wrote in message <hur5c4$fab$1@fred.mathworks.com>...
>
> "Snow White" <gulesaman@gmail.com> wrote in message
> news:hur55o$2hq$1@fred.mathworks.com...
> >P is a row vector of size 1 7749245
>
> Didn't we have this discussion earlier in the thread? Look at posts 4 and 7
> in this thread.
>
> Make A, B, C, and D column vectors.
> Combine them together to make an N-by-5 matrix p.
> Loop over the rows of p.
> Calculate the ROOTS of each row and store them in the rows of theroots.
>
> --
> 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: out of memory while using roots

From: Steven Lord

Date: 10 Jun, 2010 19:39:06

Message: 13 of 14


"Snow White" <gulesaman@gmail.com> wrote in message
news:hur9f4$ht2$1@fred.mathworks.com...
> Thank you.
>
> I have done as u had suggested but i dont understand how should i index
> the resultant theroots? i have done the following:
>
> [pr,pc]=size(p);
> for pr_index=1:pr
> theroots(:,:)= roots(p(pr_index,:)); %%computing roots of the equation
> inorder to find the refractive index
> end
> but that ofcourse only gives the last value stored in it. and the size is
> as follows:
> size(theroots(:,:))

You're almost there. On the right-hand side of the body of your FOR loop,
you extracted row pr_index of p. Now on the left-hand side you want to
insert the results into row pr_index of theroots. Since those sentences
sound somewhat similar in their descriptions, perhaps the code is similar?

theroots(pr_index, ...

--
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: out of memory while using roots

From: Snow White

Date: 10 Jun, 2010 19:55:23

Message: 14 of 14

i had already tried that :
for pr_index=1:pr
               theroots(pr_index,:)= roots(p(pr_index,:)); %%computing roots of the equation inorder to find the refractive index
            end
and i get this
??? Subscripted assignment dimension mismatch.
roots gives 4 root values for each element.

"Steven Lord" <slord@mathworks.com> wrote in message <hurf0o$q4c$1@fred.mathworks.com>...
>
> "Snow White" <gulesaman@gmail.com> wrote in message
> news:hur9f4$ht2$1@fred.mathworks.com...
> > Thank you.
> >
> > I have done as u had suggested but i dont understand how should i index
> > the resultant theroots? i have done the following:
> >
> > [pr,pc]=size(p);
> > for pr_index=1:pr
> > theroots(:,:)= roots(p(pr_index,:)); %%computing roots of the equation
> > inorder to find the refractive index
> > end
> > but that ofcourse only gives the last value stored in it. and the size is
> > as follows:
> > size(theroots(:,:))
>
> You're almost there. On the right-hand side of the body of your FOR loop,
> you extracted row pr_index of p. Now on the left-hand side you want to
> insert the results into row pr_index of theroots. Since those sentences
> sound somewhat similar in their descriptions, perhaps the code is similar?
>
> theroots(pr_index, ...
>
> --
> 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
>

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