Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.



Re: Convert from symbolic to double
Posted:
Mar 1, 2013 7:50 AM


On 01.03.13 13:00, Peter Novak wrote: > Hi > > I have n symbolic functions for 1 rectangular element. Number of nodes in this elements is 4 and I have for each node defined 3 functions. Code looks like > > x_1_n = some number > x_2_n = some number > y_1_n = some number > y_2_n = some number > > x_num = [x_1_n:1:x_2_n]; > y_num = [y_1_n:1:y_2_n]; > > n_x = length(x) > n_y = length(y) > > n = 12; % 4 nodes and 3 function for each node = 12 functions > > for h = 1:n > for i = 1:n_x > for j = 1:n_y > v_x_y_n(i,j) = subs(v_x_y(h), [x, y, x_1, x_2, y_1, y_2], [x_num(i), y_num(j), x_1_n, x_2_n, y_1_n, y_2_n]); > end > end > end > > where > v_x_y(h)....symbolic function like (1/(x_2x_1))*x+((1/(y_2y_1))*y > > I have the same for loop for substitute symbolic variables to numerical values for derivatives of functions in x and y direction and second derivatives. > > This is equation only for 1 element and program runs very slow, but I will have at the end more elements (one for loop more). > > How can I speed up my program?
It's hard to say for sure without seeing your actual code (and you'd need to break it down to something small of course), but in general, my advise is:
If you find yourself substituting values into the same expression over and over again (such as your v_x_y(h)) and you only expect double results anyway, try using matlabFunction instead of a symbolic substitution.
In your case, making use of x_1_n etc. being constant (untested code):
[X,Y] = meshgrid(x_num, y_num); for h=1:n f = matlabFunction(subs(v_x_y(h), [x_1, x_2, y_1, y_2], ... [x_1_n, x_2_n, y_1_n, y_2_n]), ... 'vars', [x, y]); v_x_y = f(X,Y); % ... I assume you do something with v_x_y here, % or assign to v_x_y(h) otherwise. end
HTH, Christopher



