Can someone please check my code?

조회 수: 2 (최근 30일)
Joseph Alcena
Joseph Alcena 2023년 5월 8일
댓글: Cris LaPierre 2023년 5월 8일
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
w = 11×11
0 0.3142 0.6283 0.9425 1.2566 1.5708 1.8850 2.1991 2.5133 2.8274 3.1416 0.3142 0.4443 0.7025 0.9935 1.2953 1.6019 1.9110 2.2214 2.5328 2.8448 3.1573 0.6283 0.7025 0.8886 1.1327 1.4050 1.6918 1.9869 2.2871 2.5906 2.8964 3.2038 0.9425 0.9935 1.1327 1.3329 1.5708 1.8318 2.1074 2.3926 2.6842 2.9804 3.2799 1.2566 1.2953 1.4050 1.5708 1.7772 2.0116 2.2654 2.5328 2.8099 3.0941 3.3836 1.5708 1.6019 1.6918 1.8318 2.0116 2.2214 2.4537 2.7025 2.9638 3.2345 3.5124 1.8850 1.9110 1.9869 2.1074 2.2654 2.4537 2.6657 2.8964 3.1416 3.3982 3.6637 2.1991 2.2214 2.2871 2.3926 2.5328 2.7025 2.8964 3.1100 3.3396 3.5820 3.8348 2.5133 2.5328 2.5906 2.6842 2.8099 2.9638 3.1416 3.3396 3.5543 3.7830 4.0232 2.8274 2.8448 2.8964 2.9804 3.0941 3.2345 3.3982 3.5820 3.7830 3.9986 4.2266
t
t = 1×21
0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000 6.0000 6.5000 7.0000 7.5000 8.0000 8.5000 9.0000 9.5000 10.0000
z
z = 1×21
0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000 6.0000 6.5000 7.0000 7.5000 8.0000 8.5000 9.0000 9.5000 10.0000
e = sin(w.*t.*z);
Arrays have incompatible sizes for this operation.
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);

답변 (1개)

Cris LaPierre
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
Joseph Alcena
Joseph Alcena 2023년 5월 8일
I see why now. Any suggestions on how I can make the w matrix the same as t and z?
Cris LaPierre
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by