# Plot Surface from Sum of Series

2 ビュー (過去 30 日間)
Azefanya 2022 年 12 月 27 日
コメント済み: Torsten 2022 年 12 月 28 日
I have a series obtained from variable separation method.
I want to plot this a surface, I tried to write this in my matlab, and got error message. I'm confused in how to make the series as a matrix not scalar so that matlab can plot it as a surface.
L = pi;
x = linspace(0,L,60);
t = [linspace(0,0.05,20), linspace(0.5,5,10)];
n = 1000 ;
for i = 1:n
u_analitik = 2/pi*((2*sin(pi*n/2)-sin(n*pi))/n^2).*(exp((-n^2)*t)*sin(n*x));
end
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix
individually, use TIMES (.*) for elementwise multiplication.
figure(2);
surf(x,t,u_analitik);

サインインしてコメントする。

### 回答 (1 件)

Karim 2022 年 12 月 27 日
Hi you need to set up a grid for each point that you want to evaluate. Afterwards you need to evaluate your function at each point and store it in a matrix. See below for a demonstration.
% define ranges for x and t
x = linspace(0,pi,60);
t = [linspace(0,0.05,20), linspace(0.5,5,10)];
% obtain the grid for the surface plot
[X,T] = meshgrid(x,t);
% initialize an array for the solution
U = zeros(size(X));
% set up the range for n
n = 1 : 1000;
for i = 1:numel(X)
% extract the current value of t and x (this is not really needed, just
xi = X(i);
ti = T(i);
U(i) = sum( 2/pi.*((2*sin(pi.*n/2)-sin(n.*pi))./n.^2).*(exp((-n.^2).*ti).*sin(n.*xi)) );
end
% create the figure
figure
surf(X,T,U)
grid on
xlabel('x'); ylabel('t'); zlabel('u')
view(3)
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Torsten 2022 年 12 月 28 日
We cannot tell what went wrong because you didn't include the code that produced the plot.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeGeometry and Mesh についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by