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:
Problem with program FEM

Subject: Problem with program FEM

From: Ricardo Tamarín

Date: 16 Sep, 2013 01:32:06

Message: 1 of 9

Hello, i have a problem with my program
function y=diri(a,b,n)
h=(b-a)./(n+1);
x=a:h:b;
f=pi^2.*sin(pi.*x);
f=@(x)pi.^2.*sin(pi.*x);
for i=1:n
   e1=(x-x(i))/(x(i+1)-x(i));
   e1=@(x) (x-x(i))/(x(i+1)-x(i));
   e2=(x(i+2)-x)/(x(i+2)-x(i+1));
   e2=@(x) (x(i+2)-x)/(x(i+2)-x(i+1));
   F=quad(f(x).*e1(x),x(i),x(i+1))+quad(f(x).*e2(x),x(i+1),x(i+2));
end

I get the following error

??? Error using ==> fcnchk at 108
FUN must be a function, a valid string expression, or an inline function object.

Error in ==> quad at 66
f = fcnchk(funfcn);

Error in ==> diri at 16
   F=quad(f(x).*e1(x),x(i),x(i+1))+quad(f(x).*e2(x),x(i+1),x(i+2));

Please help me.

Thanks.

Subject: Problem with program FEM

From: Nasser M. Abbasi

Date: 16 Sep, 2013 01:54:59

Message: 2 of 9

On 9/15/2013 8:32 PM, Ricardo Tamarín wrote:
> Hello, i have a problem with my program
> function y=diri(a,b,n)
> h=(b-a)./(n+1);
> x=a:h:b;
> f=pi^2.*sin(pi.*x);
> f=@(x)pi.^2.*sin(pi.*x);

Why would you write f=.... and then next line write f=@(x).....??

And why would you write pi.^2 instead of pi^2?
And why would you write pi.^2.*sin(...) instead of pi^2*sin(...) ?

You always put a DOT next to everything in your code?

--Nasser

Subject: Problem with program FEM

From: Ricardo Tamarín

Date: 16 Sep, 2013 02:31:07

Message: 3 of 9

"Nasser M. Abbasi" wrote in message <l15odk$rbg$1@speranza.aioe.org>...
> On 9/15/2013 8:32 PM, Ricardo Tamarín wrote:
> > Hello, i have a problem with my program
> > function y=diri(a,b,n)
> > h=(b-a)./(n+1);
> > x=a:h:b;
> > f=pi^2.*sin(pi.*x);
> > f=@(x)pi.^2.*sin(pi.*x);
>
> Why would you write f=.... and then next line write f=@(x).....??
>
> And why would you write pi.^2 instead of pi^2?
> And why would you write pi.^2.*sin(...) instead of pi^2*sin(...) ?
>
> You always put a DOT next to everything in your code?
>
> --Nasser
>

Sorry they are rest, my problem is with quad

function y=diri(a,b,n)

h=(b-a)/(n+1);
x=a:h:b;
f=@(x)pi^2*sin(pi.*x);
for i=1:n
   e1=@(x) (x-x(i))/(x(i+1)-x(i));
   e2=@(x) (x(i+2)-x)/(x(i+2)-x(i+1));
   F=quad(f(x).*e1(x),x(i),x(i+1))+quad(f(x).*e2(x),x(i+1),x(i+2));
end
   

Subject: Problem with program FEM

From: Nasser M. Abbasi

Date: 16 Sep, 2013 02:45:18

Message: 4 of 9

  
> Sorry they are rest, my problem is with quad
>
> function y=diri(a,b,n)
>
> h=(b-a)/(n+1);
> x=a:h:b;
> f=@(x)pi^2*sin(pi.*x);
> for i=1:n
> e1=@(x) (x-x(i))/(x(i+1)-x(i));
> e2=@(x) (x(i+2)-x)/(x(i+2)-x(i+1));
> F=quad(f(x).*e1(x),x(i),x(i+1))+quad(f(x).*e2(x),x(i+1),x(i+2));
> end
>
>

That is becuase you are _not_ passing a function to quad. Just look at it.
You are passing

      f(x).*e1(x)

Is this a function? No. It is a vector. It is the result of f(x)*e1(x).

--Nasser

Subject: Problem with program FEM

From: Ricardo Tamarín

Date: 16 Sep, 2013 02:59:09

Message: 5 of 9

>
> That is becuase you are _not_ passing a function to quad. Just look at it.
> You are passing
>
> f(x).*e1(x)
>
> Is this a function? No. It is a vector. It is the result of f(x)*e1(x).
>
> --Nasser
>
>

You're right, thanks, but in that case i get ??? Error using ==> mtimes
Inner matrix dimensions must agree.

Error in ==> diri at 13
   F=quad(f(x)*e1(x),x(i),x(i+1))+quad(f(x)*e2(x),x(i+1),x(i+2));

how i can fix it.

Subject: Problem with program FEM

From: Nasser M. Abbasi

Date: 16 Sep, 2013 03:08:05

Message: 6 of 9

On 9/15/2013 9:59 PM, Ricardo Tamarín wrote:
>>
>> That is becuase you are _not_ passing a function to quad. Just look at it.
>> You are passing
>>
>> f(x).*e1(x)
>>
>> Is this a function? No. It is a vector. It is the result of f(x)*e1(x).
>>
>> --Nasser
>>
>>
>

> You're right, thanks, but in that case i get ??? Error using ==> mtimes
> Inner matrix dimensions must agree.
>
> Error in ==> diri at 13
> F=quad(f(x)*e1(x),x(i),x(i+1))+quad(f(x)*e2(x),x(i+1),x(i+2));
>
> how i can fix it.
>

I think you missed the point. You changed f(x).*e1(x) to f(x)*e1(x),
but this is not the problem. The problem is not the use of dot. Now,
without the dot in place, you are doing matrix multiplication instead
of element by element multiplication.

But both of these are wrong. quad needs to have a function. Not
a vector, and not a matrix. If you look at the examples here:

http://www.mathworks.com/help/matlab/ref/quad.html

it will help.

Basically your call to quad is all wrong.

--Nasser

Subject: Problem with program FEM

From: Ricardo Tamarín

Date: 16 Sep, 2013 04:50:07

Message: 7 of 9

then I don't know how to calculate the integral of the product of functions in this case, which command do you recommend?

Subject: Problem with program FEM

From: Steven_Lord

Date: 16 Sep, 2013 15:39:07

Message: 8 of 9



"Ricardo Tamarn" <tamarin_091@hotmail.com> wrote in message
news:l162lv$6f4$1@newscl01ah.mathworks.com...
> then I don't know how to calculate the integral of the product of
> functions in this case, which command do you recommend?

You're doing the opposite of what I called out as a common mistake in this
thread:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/170614#911036

In this case, you don't want to pass in the product of the two functions as
the first input to QUAD. You want to pass in a function that when evaluated
returns the product of the values of the two functions.

f = @(x) x+1;
g = @(x) x+2;
x = 1:10;
y = f(x).*g(x) % returns a vector
h = @(x) f(x).*g(x) % returns a function handle
h(x) % returns a vector

You don't want to pass y into QUAD. You want to pass h into QUAD, so that
QUAD can evaluate h at the values of x QUAD chooses.

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

Subject: Problem with program FEM

From: Ricardo Tamarín

Date: 16 Sep, 2013 16:52:06

Message: 9 of 9


Thank you very much!!!! I was committing another error but I solved

h=(b-a)/(n+1);
x=a:h:b;
f=@(x) pi^2*sin(pi*x);
for i=1:n
   z=x(i);
   t=x(i+1);
   r=x(i+2);
   e1=@(x) (x-z)/(t-z);
   e2=@(x) (r-x)/(r-t);
   h=@(x) f(x).*e1(x);
   g=@(x) f(x).*e2(x);
   F=quad(h,z,t)+quad(g,t,r)
end

thanks, again!!

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