Arrays have incompatible sizes for this operation.

3 views (last 30 days)
Help me fix the u = ... line, please?
clear ; clc ; close all ;
k =0.3;
n=1:2;
dx =.05; % x stepsize
dt =.025; % t stepsize
x =0: dx : 1 ; % creates vector of x values
t =0: dt :.5; % creates vector of t values
[ X ,T ]= meshgrid (x , t ) ;
u = (-2.*(8.*(-7.*pi.^2.*n.^2 + 48.*n.*pi.*sin(n.*pi/2) - 96))+(pi.^4.*n.^4 - 40.*n.^2.*pi.^2 + 768).*cos(n.*pi/2)/(pi.^5.*n.^5))*sin(n.*pi.*x/2)*exp(-(n.^2.*pi.^2/4+cos(t)).*t);
Arrays have incompatible sizes for this operation.
surf (X ,T , u ) % 3D surface plot
xlabel ('x')
ylabel ('t')
zlabel ('u(x,t)')
message2show =[ 'k=' num2str ( k ) ' and L=' num2str ( L ) ];
title ( message2show )
colormap ( gray ) % change the colormap
colorbar % adds the colorbar to the sid

Answers (1)

Image Analyst
Image Analyst on 27 Feb 2023
Edited: Image Analyst on 27 Feb 2023
The problem is n is a 2 element vector while x and t are 21 elements.
Instead of one long complicated equation, break it up into 4 or 5 terms and then combine them. It will be easier to see your error. Check the size of each term and make sure you're using .* instead of * when multiplying vectors times vectors.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!