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)).
답변 (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)
카테고리
도움말 센터 및 File Exchange에서 Operations on Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!