How to fix PATTERN must be a string or cell array of strings error?

3 vues (au cours des 30 derniers jours)
Hello! I've got this code:
clear all;
x = [0.0, 6, 9, 12, 15, 18];
% Построить интерполяционный полином по значениям x и y(x) различными методами (прямой метод, Лагранжа, Ньютона)
% Построить интерполяционные сплайны второй степени.
% Методом наименьших квадратов построить аппроксимирующую функцию первой, второй и третьей степени.
yy = y(x);
% lagrange's interpolation
lagrange(x, yy, 1)
% newton's interpolation
newton(x, yy, 1)
% splines 2-order
SplineInterpolation(x, yy, 1)
% min_square_method 1-order
N = length(x);
Mx = sum(x);
Mx2 = sum(x.^2);
My = sum(yy);
Mxy = sum(x.*yy);
A = [Mx2 Mx
Mx N];
B = [Mxy; My];
X=A\B;
% ответ при х = 1
X(1)* 1 + X(2)
% рассчитываем результирующую прямую
y2= X(1).*x + X(2);
plot(x,yy,'-or',x,y2,'b');
grid on;
legend('Исходная функция','Метод наименьших квадратов');
% min_square_method 2-order
N = length(x);
Mx = sum(x);
Mx2 = sum(x.^2);
Mx3 = sum(x.^3);
Mx4 = sum(x.^4);
My = sum(yy);
Mxy = sum(x.*yy);
Mx2y = sum((x.^2).*yy);
A = [Mx4 Mx3 Mx2
Mx3 Mx2 Mx
Mx2 Mx N];
B = [Mx2y; Mxy; My];
X=A\B;
% ответ при х = 1
X(1)*1 + X(2) * 1 + X(3)
% рассчитываем результирующую прямую
y2= X(1).*(x.^2) + X(2).*x + X(3);
plot(x,yy,'-or',x,y2,'b');
grid on;
legend('Исходная функция','Метод наименьших квадратов');
function val = y(x)
val = 1 ./ x;
val(x==0)=0;
end
function yy = lagrange(x,y,xx)
N=length(x);
Nres=length(xx);
yy=zeros(size(xx));
for k=1:N
L=ones(size(xx));
for j=[1:k-1, k+1:N]
for i=[1:Nres]
L(i)=L(i).*(xx(i)-x(j))/(x(k)-x(j));
end
end
yy = yy + y(k)*L;
end
end
function yy = newton(x, y, xx)
N = length(x);
y0 = y;
for k = 1 : N-1
for i = 1 : N - k
y0(i) = (y0(i+1) - y0(i)) / (x(i+k) - x(i));
end
end
yy = y0(1) * ones(size(xx));
for k = 2 : N
yy = y0(k) + (xx - x(k)) .* yy;
end
end
function yn = SplineInterpolation(x, y, xn)
InputNum = length(x);
An = y;
Bn = zeros(InputNum, 1);
Dn = zeros(InputNum, 1);
A = zeros(InputNum,InputNum);
A(1,1) = 1;
A(InputNum,InputNum) = 1;
for i = 2:InputNum-1
h0 = x(i)-x(i-1);
h1 = x(i+1)-x(i);
a_t = [h0, 2*(h0+h1), h1];
A(i,i-1:i+1) = a_t;
end
b = zeros(InputNum, 1);
b(1) = 0;
b(InputNum) = 0;
for i = 2:InputNum-1
h0 = x(i) - x(i-1);
h1 = x(i+1) - x(i);
a0 = y(i-1);
a1 = y(i);
a2 = y(i+1);
b(i) = 3/h1*(a2-a1)-3/h0*(a1-a0);
end
% Cn = linsolve(A,b)
Cn = SolveSplineEquations(A, b, InputNum);
for i = 1:InputNum-1
hi = x(i+1)-x(i);
Bn(i) = 1/hi*(An(i+1) - An(i)) - hi/3*(2*Cn(i) + Cn(i+1));
Dn(i) = (Cn(i+1) - Cn(i))/(3*hi);
end
yn = zeros(1,length(xn));
for i = 1:length(xn)
curx = xn(i);
pos = curx>=x;
pos = strfind(pos,1);
if(isempty(pos))
PSet=1;
else
PSet = pos(end);
end
if(PSet<1)
PSet=1;
end
if(PSet>InputNum-1)
PSet=InputNum-1;
end
xi = x(PSet);
yn(i) = An(PSet) + Bn(PSet)*(curx-xi) + Cn(PSet)*(curx-xi)^2 + Dn(PSet)*(curx-xi)^3;
end
end
function x = SolveSplineEquations(A, b, InputNum)
y = zeros(InputNum, 1);
x = zeros(InputNum, 1);
Beta = zeros(InputNum, 1);
for i = 1:InputNum-1
ci = A(i, i+1);
bi = A(i, i);
if(i==1)
Beta(i) = ci/bi;
else
ai = A(i, i-1);
Beta(i) = ci/(bi - ai*Beta(i-1));
end
end
for i = 1:InputNum
bi = A(i, i);
if(i==1)
y(i) = b(i)/bi;
else
ai = A(i, i-1);
y(i) = (b(i) - ai*y(i-1))/(bi-ai*Beta(i-1));
end
end
for i = InputNum:-1:1
if(i==InputNum)
x(i) = y(i);
else
x(i) = y(i) - Beta(i)*x(i+1);
end
end
end
from my friend and tried run it in Octave but it failed with:
error: strfind: PATTERN must be a string or cell array of strings
error: called from
SplineInterpolation at line 82 column 13
task1_semester2 at line 147 column 1

Réponse acceptée

Walter Roberson
Walter Roberson le 12 Juin 2021
pos = curx>=x;
pos = strfind(pos,1);
In MATLAB, it is permitted by undocumented to pass in numeric or logical arrays to strfind. For example,
strfind(logical([1 0 0 0 1 1 1 1 0]), logical([0 1 1 1]))
is legal (but not documented.)
You used a tag of octave which suggests that you might possibly not be MATLAB. octave is a different programming language that is inspired by MATLAB, but which is permitted to have whatever incompatibilities it wants.
You could probably replace
pos = strfind(pos,1)
with
pos = find(pos);

Plus de réponses (0)

Catégories

En savoir plus sur Octave dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by