Got Questions? Get Answers.
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:
symbolic in FOR loop. not run...WHy??

Subject: symbolic in FOR loop. not run...WHy??

From: Quynh Tram Nghuyen Thi

Date: 21 Dec, 2012 18:24:13

Message: 1 of 2

Helloo all,
I may put a question about symbolic & for loop. I AM USING FOR loop to construct functions as following:

(M-FILE)
 syms x y
    for i=1:4
        N(i,1)=(x-1)*(y-1);N(i+4,1)=(x-5+i)*(y-2);
        N(i,2)=-(x-i+1)*(y-1);N(i+4,2)=-(x-4+i)*(y-2);
        N(i,3)=(x-i+1)*(y);N(i+4,3)=(x-4+i)*(y-1);
        N(i,4)=-(x-1)*(y);N(i+4,4)=(x-5+i)*(y-1);
        
    end
(RUNNING ERROR)
? The following error occurred converting from sym to
double:
Error using ==> sym.double at 25
DOUBLE cannot convert the input expression into a double
array.
If the input expression contains a symbolic variable, use
the VPA function instead.

Error in ==> poiosnfem_trial at 41
        N(i,1)=(x-1)*(y-1);N(i+4,1)=(x-5+i)*(y-2);


WHY?? I DONT UNDERSTAND AT ALL. please, ANY IDEAS!

Subject: symbolic in FOR loop. not run...WHy??

From: Steven_Lord

Date: 31 Dec, 2012 05:48:22

Message: 2 of 2



"Quynh Tram Nghuyen Thi" <lovelyeverything@yahoo.com.vn> wrote in message
news:kb29gd$ltb$1@newscl01ah.mathworks.com...
> Helloo all,
> I may put a question about symbolic & for loop. I AM USING FOR loop to
> construct functions as following:
>
> (M-FILE)
> syms x y
> for i=1:4
> N(i,1)=(x-1)*(y-1);N(i+4,1)=(x-5+i)*(y-2);

*snip*

If N is a double array already, this will need to convert (x-1)*(y-1) into a
double precision value in order to store that value in an element of N.
However, since you haven't specified a value for x or y, it cannot perform
that conversion and so throws the error. Either specify NUMERIC values for x
and y in your script/function [so that (x-1)*(y-1) results in a numeric
value which can be converted to a double precision value] or initialize N
symbolically before running this loop so MATLAB doesn't need to convert the
symbolic (x-1)*(y-1) to perform the assignment.

N = sym(zeros(4, 4));

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with 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