Can someone please check my code?
2 次查看(过去 30 天)
显示 更早的评论
I'm trying to plot the wave equation in 3D but I'm getting an error with the array size for e.
This is the error it's giving me:
"Arrays have incompatible sizes for this operation. Error in PHYS225_Project (line 36) e = sin(w.*t.*z);"
If anyone can help me make my code work I'f greatly appreicate it.
Here's my code:
clear all
clc
close all
% Define the parameters
xo = 1;
yo = 1;
v = 5;
Lx = 50;
Ly = 50;
n = 0:1:10;
m = 0:1:10;
% Compute the wave vector and frequency
[kx, ky] = meshgrid(n*pi/Lx, m*pi/Ly);
w = v * sqrt(kx.^2 + ky.^2);
% Define the time and space grids
t = 0:0.5:10;
z = 0:0.5:10;
[x, y] = meshgrid(0:Lx/50:Lx, 0:Ly/50:Ly);
% Compute the wave equation solution
a = sin(n*pi*xo/Lx);
b = sin(m*pi*yo/Ly);
a_mat = repmat(a.', [1, length(m)]);
b_mat = repmat(b, [length(n), 1]);
% c = sin(kx.*x);
c = sin(reshape(x,1,[]).'*kx(:).');
% d = sin(ky.*y);
d = sin(reshape(y,1,[]).'*ky(:).');
w
t
z
e = sin(w.*t.*z);
u = a_mat.*b_mat.*c.*d.*e;
% Plot the solution
figure(1);
subplot(211);
surf(x, y, u(:, :, 1));
xlabel('x');
ylabel('y');
zlabel('u');
title('Wave equation solution at t = 0');
axis([0 Lx 0 Ly -1 1]);
colormap(jet);
subplot(212);
surf(x, y, u(:, :, end));
xlabel('x');
ylabel('y');
zlabel('u');
title(['Wave equation solution at t = ' num2str(t(end))]);
axis([0 Lx 0 Ly -1 1]);
colormap(jet);
0 个评论
回答(1 个)
Cris LaPierre
2023-5-8
I've edited your code to make the error more visible.
Can you now see why w.*t.*z is giving your an error that the arrays contain incompatible size for this operation?
2 个评论
Cris LaPierre
2023-5-8
Since you ultimately want to use surf to visualize your data, I would think the best approach is to make them all 11x11, but you would know better than me if that is correct.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!