I continue to get this error and can't figure out how to fix it, Unable to perform assignment because the size of the left side is 1-by-4 and the size of the right side is 1-b
1 view (last 30 days)
Show older comments
% Given temperature-depth data
z = [0, -2.3, -4.6, -6.9, -9.2, -11.5, -13.8, -16.1]; % Depth in meters
T = [22.8, 22.7, 22.5, 20.6, 13.9, 11.7, 11.2, 11.1]; % Temperature in Celsius
% Step 1: Plot the data points
figure;
plot(T, z, 'r*'); % Plotting temperature vs depth with red stars
xlabel('Temperature (°C)');
ylabel('Depth (m)');
title('Temperature vs Depth');
set(gca, 'YDir','reverse'); % Inverting the y-axis
grid on;
% Step 2: Sort the data since the depth must be in increasing order
[z_sorted, sortIndex] = sort(z);
T_sorted = T(sortIndex);
% Step 3: Create a cubic spline interpolation
cs = spline(z_sorted, T_sorted);
% Step 4: Evaluate the spline and its derivatives on a finer grid
z_fine = linspace(min(z_sorted), max(z_sorted), 500);
T_spline = ppval(cs, z_fine);
% Step 5: Find the first and second derivatives of the spline
[breaks,coefs,l,k,d] = unmkpp(cs); % Extracts the pieces of the cubic spline
dcoefs = coefs; % Derivative coefficients
% Each row of dcoefs will be the coefficients of the polynomial of a piece
for j = 1:l
dcoefs(j,:) = polyder(dcoefs(j,:));
end
% Make a pp-form of derivative
csd1 = mkpp(breaks,dcoefs(:,1:k-1));
% First derivative evaluation
T_spline_deriv = ppval(csd1, z_fine);
% Find the second derivative
for j = 1:l
dcoefs(j,:) = polyder(dcoefs(j,:));
end
% Make a pp-form of second derivative
csd2 = mkpp(breaks,dcoefs(:,1:k-2));
% Second derivative evaluation
T_spline_second_deriv = ppval(csd2, z_fine);
% Step 6: Locate the thermocline by finding the depth where the second derivative
% changes sign, and the first derivative is a maximum
inflection_points = find(diff(sign(T_spline_second_deriv)) ~= 0) + 1;
[~, max_gradient_index] = max(abs(T_spline_deriv(inflection_points)));
thermocline_depth = z_fine(inflection_points(max_gradient_index));
thermocline_temperature = T_spline(inflection_points(max_gradient_index));
% Display the thermocline depth and temperature
fprintf('The thermocline is located at a depth of %.2f m with a temperature of %.2f°C.\n', ...
thermocline_depth, thermocline_temperature);
0 Comments
Answers (1)
Torsten
on 29 Sep 2024
Edited: Torsten
on 29 Sep 2024
If you take the derivative of a polynomial, the degree of the resulting polynomial is one less than the degree of the original polynomial. Since "dcoeffs" is a 7x4 array and the result of polyder(dcoefs(j,:)) will be a 1x3 vector for each j, you try to save a 1x3 vector within a still existing 7x4 matrix - dimension mismatch.
Solution: Save the coefficients of the first and second derivatives in new 7x3 and 7x2 matrices, respectively.
See Also
Categories
Find more on Spline Postprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!