Error at , can not run M = A\b;

2 vues (au cours des 30 derniers jours)
JAMEEL
JAMEEL le 24 Nov 2023
Déplacé(e) : Stephen23 le 16 Sep 2024
% Given data
x = [2.0, 3.0, 6.5, 8.0, 12, 15];
f = [14, 20, 17, 16, 23, 125];
% Construct the cubic spline
h = diff(x);
A = zeros(length(x)-2);
for i = 1:length(x)-2
A(i,i) = 2*h(i) + 2*h(i+1);
A(i,i+1) = h(i+1);
A(i+1,i) = h(i);
A(i+1,i+1) = 2*h(i) + h(i+1);
end
b = [6*(f(2)-f(1))/h(1) + 6*(f(3)-f(2))/h(2);
6*(f(4)-f(3))/h(3) + 6*(f(5)-f(4))/h(4)];
M = A\b;
% Evaluate the second derivative at data points
d2f_dx2 = zeros(length(x),1);
for i = 1:length(x)-2
d2f_dx2(i) = M(i);
d2f_dx2(i+1) = M(i) + h(i)*M(i+1);
d2f_dx2(i+2) = M(i) + 2*h(i)*M(i+1) + h(i)*h(i)*M(i+2);
end
% Display second derivatives at data points
disp('Second Derivatives at Data Points:');
disp(d2f_dx2);

Réponses (2)

Omar
Omar le 25 Nov 2023
Déplacé(e) : Stephen23 le 16 Sep 2024
The MATLAB code you provided is intended to construct a cubic spline and calculate the second derivatives at data points. However, there are a few issues in the code which could be causing the error.
1. Matrix A Construction: The loop for constructing matrix `A` is not set up correctly. The current loop may not fill the matrix `A` properly, especially the last row and column.
2. Vector b Construction: The vector `b` is not constructed correctly. The current implementation only considers two values, but it should consider all the interior points.
3. Calculating Second Derivatives: The loop for calculating the second derivatives (`d2f_dx2`) is not correctly set up and might result in an index out-of-bounds error.
Here's the corrected MATLAB code:
% Given data
x = [2.0, 3.0, 6.5, 8.0, 12, 15];
f = [14, 20, 17, 16, 23, 125];
% Construct the cubic spline
h = diff(x);
n = length(x) - 2;
A = zeros(n, n);
b = zeros(n, 1);
% Constructing matrix A
for i = 1:n
if i < n
A(i, i+1) = h(i+1);
A(i+1, i) = h(i+1);
end
A(i, i) = 2 * (h(i) + h(i+1));
end
% Constructing vector b
for i = 1:n
b(i) = 6 * ((f(i+2) - f(i+1)) / h(i+1) - (f(i+1) - f(i)) / h(i));
end
% Solve for M
M = A\b;
% Evaluate the second derivative at data points
d2f_dx2 = zeros(length(x), 1);
d2f_dx2(2:end-1) = M;
% Display second derivatives at data points
disp('Second Derivatives at Data Points:');
disp(d2f_dx2);
**Please accept the answer if you like it and it runs well. It will increase my rank.

Namnendra
Namnendra le 16 Sep 2024
Hi Jameel,
The error you're encountering with `M = A\b;` suggests that there is an issue with the matrix `A` or the vector `b`. In your code, you are trying to solve a system of linear equations to find the coefficients for a cubic spline interpolation. Let's address the issues step by step.
Issues in the Code
1. Matrix Dimensions:
- The matrix `A` is not being constructed correctly. The loop over `i` attempts to access `A(i+1, i+1)` even when `i` is at its maximum value, which leads to an out-of-bounds error.
- The vector `b` is hardcoded for a specific size, which might not match the size of `A`.
2. Boundary Conditions:
- Cubic splines typically require boundary conditions to be specified. These conditions affect the first and last rows of matrix `A` and elements of vector `b`.
Corrected Code
Here's a revised version of your code that correctly constructs the cubic spline matrix and vector:
% Given data
x = [2.0, 3.0, 6.5, 8.0, 12, 15];
f = [14, 20, 17, 16, 23, 125];
% Number of intervals
n = length(x) - 1;
% Construct the cubic spline matrix
h = diff(x);
A = zeros(n-1, n-1);
b = zeros(n-1, 1);
for i = 1:n-1
if i > 1
A(i, i-1) = h(i);
end
A(i, i) = 2 * (h(i) + h(i+1));
if i < n-1
A(i, i+1) = h(i+1);
end
b(i) = 6 * ((f(i+2) - f(i+1)) / h(i+1) - (f(i+1) - f(i)) / h(i));
end
% Solve for the second derivatives
M = A\b;
% Add boundary conditions (natural spline: second derivatives at boundaries are zero)
M = [0; M; 0];
% Evaluate and display the second derivative at data points
disp('Second Derivatives at Data Points:');
disp(M);
Explanation
- Matrix `A` Construction: The loop now correctly fills the tridiagonal matrix `A` for the cubic spline. It ensures that off-diagonal elements are only set when valid indices are available.
- Boundary Conditions: This example assumes a natural spline, where the second derivatives at the endpoints are zero. Therefore, `M` is extended with zeros at both ends.
- Vector `b` Construction: The vector `b` is constructed based on the differences in the function values, ensuring it matches the size of `A`.
By applying these corrections, you should be able to solve the system of equations without errors and obtain the second derivatives for your cubic spline interpolation.
Thank you.

Catégories

En savoir plus sur Spline Postprocessing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by