Substuting values into symbolic expression to create 3D array.
Show older comments
I have a symbolic expression that takes in 3 symbolic inputs. I want to substitute 3 vectors of inputs into that symbolic expression to create a 3D array of the values of the function at all points in the input.
u(x,y,t) = cos(2*t - 2*x)*cos(2*t - 2*y);
x_vals = 0:0.1:1;
y_vals = 0:0.1:1;
t_vals = 0:0.1:1;
In this example I would like to create a array with dimensions defined by the sizes of the arrays x_vals, y_vals, and t_vals, called vals. So for example vals(i,j,k) would be u(x_vals(i),y_vals(j),t_vals(k)).
Answers (1)
Either work grid-wise (easier), or else do one step at a time making sure to reshape the vector into the proper shape
syms x y t
u(x,y,t) = cos(2*t - 2*x)*cos(2*t - 2*y);
x_vals = 0:0.1:1;
y_vals = 0:0.1:1;
t_vals = 0:0.1:1;
[X,Y,T] = ndgrid(x_vals, y_vals, t_vals);
U = u(X, Y, T);
U(1,:,1)
t1 = u(x_vals(:), y, t); size(t1)
t2 = subs(t1, y, y_vals); size(t2)
t3 = subs(t2, t, reshape(t_vals,1,1,[])); size(t3)
t3(1,:,1)
Categories
Find more on Operations on Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!