Substuting values into symbolic expression to create 3D array.

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)).

Respuestas (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)
ans = 
t1 = u(x_vals(:), y, t); size(t1)
ans = 1×2
11 1
t2 = subs(t1, y, y_vals); size(t2)
ans = 1×2
11 11
t3 = subs(t2, t, reshape(t_vals,1,1,[])); size(t3)
ans = 1×3
11 11 11
t3(1,:,1)
ans = 

Productos

Versión

R2020a

Preguntada:

el 12 de Dic. de 2021

Respondida:

el 12 de Dic. de 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by