Vectors must be same length

1 visualización (últimos 30 días)
Aijalon Marsh
Aijalon Marsh el 4 de Oct. de 2023
Respondida: 檮杌 el 4 de Oct. de 2023
% Define constants
L = 50; % Length of the plate in cm
W = 30; % Width of the plate in cm
T_top = 85; % Temperature at the top side in °C
T_sides = 25; % Temperature at the left, bottom, and right sides in °C
accuracy = 1e-2; % Desired accuracy
% Define the points where temperature needs to be calculated
points = [L/4, W/4; 3*L/4, W/4; L/2, W/2; L/4, 3*W/4; 3*L/4, 3*W/4];
% Initialize variables
T = zeros(size(points, 1), 1);
n = 1;
accuracy_met = false;
% Calculate temperature at each point using the infinite series
while ~accuracy_met
T_old = T;
for i = 1:size(points, 1)
x = points(i, 1);
y = points(i, 2);
% Calculate the temperature at the current point (x, y)
T(i) = T_sides + 4 * T_top / pi;
for m = 1:0.01:10 % Considering odd terms in the series
T(i) = T(i) + (4 * T_top / (pi * m)) * sinh(m * pi * x / L) * sin(m * pi * y / W);
end
end
% Check for accuracy
max_diff = max(abs(T - T_old));
if max_diff < accuracy
accuracy_met = true;
end
n = n + 1;
end
% Display the number of terms required for the desired accuracy
fprintf('Number of terms required for accuracy of %.2f°C: %d\n', accuracy, n);
Number of terms required for accuracy of 0.01°C: 3
% Plot Temperature vs. Number of Terms
figure;
plot(1:n, T, '-o');
Error using plot
Vectors must be the same length.
xlabel('Number of Terms');
ylabel('Temperature (°C)');
title('Temperature vs. Number of Terms');
grid on;
% Display the solution in tabular form
results = [points, T];
disp('Point (x, y) Temperature (°C)');
disp(results);

Respuesta aceptada

檮杌
檮杌 el 4 de Oct. de 2023
The error occurs because n=3 but T is a 1x5 vector.
% Define constants
L = 50; % Length of the plate in cm
W = 30; % Width of the plate in cm
T_top = 85; % Temperature at the top side in °C
T_sides = 25; % Temperature at the left, bottom, and right sides in °C
accuracy = 1e-2; % Desired accuracy
% Define the points where temperature needs to be calculated
points = [L/4, W/4; 3*L/4, W/4; L/2, W/2; L/4, 3*W/4; 3*L/4, 3*W/4];
% Initialize variables
T = zeros(size(points, 1), 1);
n = 1;
accuracy_met = false;
% Calculate temperature at each point using the infinite series
while ~accuracy_met
T_old = T;
for i = 1:size(points, 1)
x = points(i, 1);
y = points(i, 2);
% Calculate the temperature at the current point (x, y)
T(i) = T_sides + 4 * T_top / pi;
for m = 1:0.01:10 % Considering odd terms in the series
T(i) = T(i) + (4 * T_top / (pi * m)) * sinh(m * pi * x / L) * sin(m * pi * y / W);
end
end
% Check for accuracy
max_diff = max(abs(T - T_old));
if max_diff < accuracy
accuracy_met = true;
end
n = n + 1;
end
% Display the number of terms required for the desired accuracy
fprintf('Number of terms required for accuracy of %.2f°C: %d\n', accuracy, n);
Number of terms required for accuracy of 0.01°C: 3
% Plot Temperature vs. Number of Terms
figure;
plot(1:length(T), T, '-o');
xlabel('Number of Terms');
ylabel('Temperature (°C)');
title('Temperature vs. Number of Terms');
grid on;
% Display the solution in tabular form
results = [points, T];
disp('Point (x, y) Temperature (°C)');
Point (x, y) Temperature (°C)
disp(results);
1.0e+12 * 0.0000 0.0000 0.0000 0.0000 0.0000 3.7067 0.0000 0.0000 0.0012 0.0000 0.0000 -0.0000 0.0000 0.0000 -2.0052

Más respuestas (0)

Categorías

Más información sobre Numerical Integration and Differential Equations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by