MATLAB Answers

'Assignment between unlike types is not allowed'

15 views (last 30 days)
Kaushal Sorte
Kaushal Sorte on 28 Oct 2020
This is a code i am trying to write for cubic spline interpolation between 4 points...I want to eventually form and plot the three cubic polynomials, but I am getting the error of ' Assignment between unlike types is not allowed' in line 30 i.e when I am defining S(k) in the last for loop. What is going wrong? Would somebody comment? A, B, C are arrays of the coefficients for the 3 polynomials.
syms x X Y
X = [0 1 2 3]
Y = [0 0.5 2 1.5]
n = length(X)
A = sym('A', [1 n]);
B = sym('B', [1 n-1]);
C = sym('C', [1 n-1]);
H = [];
for i = 1:n-1
H(i) = X(i+1) - X(i);
end
for k = 1:n-2
eqns(k) = H(k)*A(k) + 2*(H(k)+H(k+1))*A(k+1) + H(k+1)*A(k+2) == (Y(k+2)-Y(k+1))/H(k+1)-(Y(k+1)-Y(k))/H(k);
end
%for natural spline
eqns(n-1) = A(1)==0;
eqns(n) = A(n)==0 ;
eqns
S = solve(eqns)
A(1) = S.A1;
A(2) = S.A2; A(3) = S.A3; A(4) = S.A4
for k = 1:n-1
B(k) = Y(k)/H(k) - A(k)*H(k);
C(k) = Y(k+1)/H(k) - A(k+1)*H(k);
end
B
C
hold on
for k = 1:n-1
S(x) = A(k)*(X(k+1)-x)^3/H(k) + A(k+1)*(x-X(k))^3/H(k) + B(k)*(X(k+1)- x) + C(k)*(x - X(k))
fplot(S(x), [X(k) X(k+1)])
end
hold off

Answers (1)

Sahithi Kanumarlapudi
Sahithi Kanumarlapudi on 24 Feb 2021
Hi,
In your code 'S' is a struct with 4 fileds and 'A(k)*(X(k+1)-x)^3/H(k) + A(k+1)*(x-X(k))^3/H(k) + B(k)*(X(k+1)- x) + C(k)*(x - X(k))' is a symbolic expression. You may assign the expression to another filed of S but not to S(x) as 'S' is struct.
Hopen this helps!

Community Treasure Hunt

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

Start Hunting!