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!