Arrays have incompatible sizes for this operation.

5 views (last 30 days)
Hello guys , I need help to fix my code . "It is giving me an error of
Error in untitled8 (line 19)"
A = diag(alpha, -1) + diag(beta) + diag(gamma, 1);
The final plot should show an ellipse with 8 points . Generally I want to create a parametric cubic spline with boundary conditions to this ellipse.
Any suggestions will be highly appreciated .
Here is my code (script below)
% Number of points on the ellipse
N = 8;
% Parametric representation of the ellipse
theta = linspace(0, 2*pi, N);
x = cos(theta);
y = sqrt(2)*sin(theta);
% Periodic parametric cubic spline interpolation
t = linspace(0, 2*pi, 1000);
ti = linspace(0, 2*pi, N);
% Build the tridiagonal system for solving the cubic spline coefficients
h = diff(ti);
alpha = 1/6 * h;
beta = (h(1:end-1) + h(2:end)) / 3;
gamma = 1/6 * h(2:end);
A = diag(alpha, -1) + diag(beta) + diag(gamma, 1);
Arrays have incompatible sizes for this operation.
A(1, N) = 1/6 * h(end);
A(N, 1) = 1/6 * h(1);
% Compute second derivatives (spline coefficients)
b = 6 * diff(diff([x, x(1)])./h);
c = A\b';
% Evaluate the cubic spline at the specified points
spline_x = zeros(1, length(t));
spline_y = zeros(1, length(t));
for i = 1:N
indices = (t >= ti(i)) & (t < ti(i+1) | (i == N && t == 2*pi));
dt = t(indices) - ti(i);
spline_x(indices) = c(i) / 6 * (ti(i+1) - dt).^3 + c(i+1) / 6 * dt.^3 + (x(i) - c(i) * h(i)^2 / 6) * (ti(i+1) - dt) + (x(i+1) - c(i+1) * h(i)^2 / 6) * dt;
spline_y(indices) = c(i) / 6 * (ti(i+1) - dt).^3 + c(i+1) / 6 * dt.^3 + (y(i) - c(i) * h(i)^2 / 6) * (ti(i+1) - dt) + (y(i+1) - c(i+1) * h(i)^2 / 6) * dt;
end
% Plot the results
figure;
plot(x, y, 'o', 'MarkerSize', 10, 'DisplayName', 'Ellipse Points');
hold on;
plot(spline_x, spline_y, 'LineWidth', 2, 'DisplayName', 'Periodic Cubic Spline Interpolation');
axis equal;
legend;
title('Periodic Parametric Cubic Spline Interpolation on Ellipse');
xlabel('x');
ylabel('y');
grid on;

Accepted Answer

Walter Roberson
Walter Roberson on 1 Dec 2023
% Number of points on the ellipse
N = 8;
% Parametric representation of the ellipse
theta = linspace(0, 2*pi, N);
x = cos(theta);
y = sqrt(2)*sin(theta);
% Periodic parametric cubic spline interpolation
t = linspace(0, 2*pi, 1000);
ti = linspace(0, 2*pi, N);
% Build the tridiagonal system for solving the cubic spline coefficients
h = diff(ti);
alpha = 1/6 * h;
beta = (h(1:end-1) + h(2:end)) / 3;
gamma = 1/6 * h(2:end);
whos alpha beta gamma
Name Size Bytes Class Attributes alpha 1x7 56 double beta 1x6 48 double gamma 1x6 48 double
A = diag(alpha, -1) + diag(beta) + diag(gamma, 1);
Arrays have incompatible sizes for this operation.
alpha is 1 x 7. To put it one off the diagonal requires that the array be 8 x 8.
beta is 1 x 6. To put it on the main diagonal requries that the array be 6 x 6.
gamm is 1 x 6. To put it one of the diagonal requires that the array be 7 x 7.
So you are trying to add together an 8 x 8 with a 6 x 6 and a 7 x 7.

More Answers (0)

Categories

Find more on Spline Postprocessing in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!