Arrays have incompatible sizes for this operation , ERROR help me fix the code
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
% Define the variables
clc
w = 2*pi; % angular frequency
L = 1; % length of domain
f = @(x) x.^2; % function to be expanded
g = @(x) x; % another function to be expanded
% Define the function y(x,t)
% Calculate the A_n coefficients
n = 20; % number of terms in the expansion
A = zeros(1, n);
for i = 1:n
A(i) = 2/L*integral(@(x) f(x).*sin(i*pi*x/L), 0, L);
end
% Calculate the B_n coefficients
B = zeros(1, n);
for i = 1:n
B(i) = 2/(w*L)*integral(@(x) g(x).*sin(i*pi*x/L), 0, L);
end
% Define the function y(x,t)
y = @(x,t)sum(((A.*cos(w*t.*(1:n)) + B.*sin(w*t.*(1:n))).*sin(((1:n).*pi.*x/L))));
% Plot the function over a range of x and t values
x = linspace(0, L, 100);
t = linspace(0, 10, 100);
[X,T] = meshgrid(x,t);
Y = y(X,T);
surf(X,T,Y);
xlabel('x'); ylabel('t'); zlabel('y(x,t)');
0 Kommentare
Akzeptierte Antwort
Abhinav
am 19 Jan. 2023
Bearbeitet: Abhinav
am 19 Jan. 2023
The line
y = @(x,t)sum(((A.*cos(w*t.*(1:n)) + B.*sin(w*t.*(1:n))).*sin(((1:n).*pi.*x/L))))
is cause of the error, t is a 1x100 array you multiplying it with (1:n) (which is 1x20 array) that is why you are getting the matrix dimension mismatch error. Changing n to 100 resolves the error.
Refer to the following documentation link to learn more about compatible array sizes for basic operation
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!