Cubic Spline not printing

1 visualizzazione (ultimi 30 giorni)
Muhammad Talha Shoaib
Muhammad Talha Shoaib il 19 Dic 2022
Modificato: John D'Errico il 4 Feb 2023
My cubic spline not working due to error in line 54 please tell me so that I can print the spline
x=[1 2 6 3 5 6 7 8 9 7 6 5 54 4 34 4 5 5 6 7 7 76 6 5 54 44];
f=[7 3 5 6 5 6 87 6 6 65 6 6 7 7 87 67 6 5 44 5 6 6 7 7 6 5];
N = length(x)-1;
x_1=zeros(1,N+1);
a = zeros(1,N+1);
for i=1:N+1
x_1(i) = x(i);
a(i)=f(i);
end
m = N-1;
h = zeros(1,m+1);
for i=1:m
h(i)=x(i+1)-x(i);
end
b = zeros(1,m);
for k=2:N
b(k)=((3/h(k))*a(k+1)-a(k))-((3/h(k-1))*a(k)-a(k-1));
end
A = zeros(1,N+1);
A(1,1)=1;
A(N+1,N+1)=1;
for l=1:N+1
for m=1:N+1
A(m,l)= 0;
end
end
for n=1:N+1
for p=2:N
if p==n
A(p,n)=2*(h(p-1)+h(p));
if p==n+1
A(p,n)=h(p-1);
if p==n-1
A(p,n)=h(p);
end
end
end
end
end
C = A\b';%Error in this line
BT=zeros(1,N+1);
D=zeros(1,N+1);
for i=1:N
BT=((1/h(i))*(a(i+1)-a(i)))-((h(i)/3)*(2*C(i)+C(i+1)));
D=(C(i+1)-C(i))/(3*h(i));
end
for i=1:N
Y=x(i):0.01:x(i+1);
S=a(i)+BT(i)*(Y-x(i))+C(i)*((Y-x(i)).^2)+D(i)*((Y-x(i)).^3);
plot(Y,S)
hold on
end
  2 Commenti
Jan
Jan il 19 Dic 2022
Modificato: Jan il 19 Dic 2022
Whenever you mention an error, post a copy of the complete message also. This helps to solve your problem, because suggesting improvements is easier than guessing, what the error is.
Please format your code properly using the tools above the field for editing.
By the way, you can simplify:
x_1=zeros(1,N+1);
a = zeros(1,N+1);
for i=1:N+1
x_1(i) = x(i);
a(i)=f(i);
end
to:
x_1 = x;
a = f;
and
h = zeros(1,m+1);
for i=1:m
h(i)=x(i+1)-x(i);
end
to:
h = [diff(x), 0];
In this part:
A = zeros(1,N+1);
A(1,1)=1;
A(N+1,N+1)=1;
for l=1:N+1
for m=1:N+1
A(m,l)= 0;
end
end
the created 1's are overwritten, so implify this to: A = zeros(N+1, N+1).
This section:
for p=2:N
if p==n
A(p,n)=2*(h(p-1)+h(p));
if p==n+1
A(p,n)=h(p-1);
if p==n-1
A(p,n)=h(p);
end
end
end
end
cannot work properly: The check for p==n+1 is inside the branch for p==n and inconsequence it is never true. I assume you meant:
for p=2:N
if p==n
A(p,n)=2*(h(p-1)+h(p));
elseif p==n+1
A(p,n)=h(p-1);
elseif p==n-1
A(p,n)=h(p);
end
end
This does not need a loop, because it can be simplified:
A(n,n) = 2 * (h(n-1) + h(n));
A(n+1,n) = h(n);
A(n-1,n) = h(n-1);
This part looks strange also:
BT=zeros(1,N+1);
D=zeros(1,N+1);
for i=1:N
BT=((1/h(i))*(a(i+1)-a(i)))-((h(i)/3)*(2*C(i)+C(i+1)));
D=(C(i+1)-C(i))/(3*h(i));
end
BT and D are overwritten in each iteration. Do you mean BT(i)= ... and D(i)=... ?
Fix the inconsistencies and simplify the code, because this makes debugging much easier.
John D'Errico
John D'Errico il 19 Dic 2022
Besides all of the many problems Jan saw, a serious one may simply be in your data.
x=[1 2 6 3 5 6 7 8 9 7 6 5 54 4 34 4 5 5 6 7 7 76 6 5 54 44];
f=[7 3 5 6 5 6 87 6 6 65 6 6 7 7 87 67 6 5 44 5 6 6 7 7 6 5];
plot(x,f,'-o')
So your data does not represent a single valued function. Yet you seem to be trying to build an interpolating spline. I won't seriously try to debug completely uncommented code, even if I have been writing spline code for over 40 years.
An interpolating spline on this data must fail. Where would I see a failure? In the call to backslash, due to a singular matrix. And what have you indicated?
C = A\b';%Error in this line

Accedi per commentare.

Risposte (1)

John D'Errico
John D'Errico il 4 Feb 2023
Modificato: John D'Errico il 4 Feb 2023
It has been a month since you asked the question, so I'll post an answer.
Note that your data has
x=[1 2 6 3 5 6 7 8 9 7 6 5 54 4 34 4 5 5 6 7 7 76 6 5 54 44];
f=[7 3 5 6 5 6 87 6 6 65 6 6 7 7 87 67 6 5 44 5 6 6 7 7 6 5];
Do you see that your data has replicate points with not even the same value for x? For example, how many data points have the value of x==5?
[xsort,tags] = sort(x);
fsort = (tags);
[xsort;fsort]'
ans = 26×2
1 1 2 2 3 4 4 14 4 16 5 5 5 12 5 17 5 18 5 24
[x(x==5);f(x==5)]
ans = 2×5
5 5 5 5 5 5 6 6 5 7
So there are how many data points, for example, where x==5? I count 5 places where x==5. How many differernt values does f take on at those points? 5 distinct values. An interpolating spline must pass through the data points. But when your function is not single valued, then it MUST fail. Did you get a singular matrix warning? That is a reflection that your data cannot be interpolated by an interpolating spline.

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by