Can someone please check my code?
2 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Cris LaPierre
on 8 May 2023
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 Comments
Cris LaPierre
on 8 May 2023
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.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!