Arrays have incompatible sizes for this operation.
Afficher commentaires plus anciens
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);
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;
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!