Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Speed up fairly short code
Replies: 9   Last Post: Jul 17, 2013 4:48 PM

 Messages: [ Previous | Next ]
 Steven Lord Posts: 18,038 Registered: 12/7/04
Re: Speed up fairly short code
Posted: Jul 17, 2013 10:00 AM

"Kobye " <kobye.bodjona@gmail.com> wrote in message
news:ks40hq\$b4m\$1@newscl01ah.mathworks.com...
> Steve, I'm quite unsure about how one would proceed with this. First of
> all, I guess I would have to use integral2 since I'm performing a surface
> integral. Second of all, it doesn't seem like MATLAB likes numerically
> integrating a matrix per entry. I was trying the below example as a
> demonstrator.
>
> a=[x^2,y^3;x*y,2*y*x^2]
>
> b=matlabFunction(a)
>
> q = integral2(b,0,1,0,1)
>
> I get a bunch of errors. I have no doubt that I can extract each entry
> from the matrix individually, define it as a function, and then integrate
> it numerically. However, there are so many operations here that it seems
> at first glance it wouldn't be efficient.

I wouldn't be so sure. The cost of N matrix entry extractions plus N numeric
integrations may still be less than the cost of N symbolic integrations,
particularly if your functions are complicated. For this simple example the
symbolic approach wins; for a more complicated function I expect it may be
different.

syms x y
a=[x^2,y^3;x*y,2*y*x^2];
tic
q = zeros(size(a));
for k = 1:numel(a)
b = matlabFunction(a(k), 'vars', [x y]);
q(k) = integral2(b, 0, 1, 0, 1);
end
toc

--
Steve Lord
slord@mathworks.com
http://www.mathworks.com

Date Subject Author
7/15/13 Kobye
7/16/13 Derek Goring
7/16/13 Kobye
7/16/13 Kobye
7/16/13 Steven Lord
7/16/13 EBS
7/17/13 Steven Lord
7/16/13 Kobye
7/17/13 Steven Lord
7/17/13 Kobye