Out of memory. The likely cause is an infinite recursion within the program. Error in polyfit (line 3) p = polyfit(x,y,2);

조회 수: 4 (최근 30일)
Hello I get this error when trying to run my code," Out of memory. The likely cause is an infinite recursion within the program." Any idea on how to fix this? My code is below.
t = [0:1:20];
Ca = [10,9.090909,8.333333,7.692308,7.142857,6.666667,6.25,5.882353,5.555556,5.263158,5,4.761905,4.545455,4.347826,4.166667,4,3.846154,3.703704,3.571429,3.448276,3.333333];
logCa = log(Ca);
coeff = polyfit(t,logCa,1)

답변 (1개)

Mathieu NOE
Mathieu NOE 2021년 12월 1일
No issue so far on my R2020b
copy pasted your data in my demo code and worked without issue :
clc
clearvars
% data
x = [0:1:20];
Ca = [10,9.090909,8.333333,7.692308,7.142857,6.666667,6.25,5.882353,5.555556,5.263158,5,4.761905,4.545455,4.347826,4.166667,4,3.846154,3.703704,3.571429,3.448276,3.333333];
y = log(Ca);
% Fit a polynomial p of degree "degree" to the (x,y) data:
degree = 1;
p = polyfit(x,y,degree);
% Evaluate the fitted polynomial p and plot:
f = polyval(p,x);
eqn = poly_equation(flip(p)); % polynomial equation (string)
Rsquared1 = my_Rsquared_coeff(y,f); % correlation coefficient
figure(1);plot(x,y,'o',x,f,'-')
legend('data',eqn)
title(['Data fit - R squared = ' num2str(Rsquared1)]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Rsquared = my_Rsquared_coeff(data,data_fit)
% R2 correlation coefficient computation
% The total sum of squares
sum_of_squares = sum((data-mean(data)).^2);
% The sum of squares of residuals, also called the residual sum of squares:
sum_of_squares_of_residuals = sum((data-data_fit).^2);
% definition of the coefficient of correlation is
Rsquared = 1 - sum_of_squares_of_residuals/sum_of_squares;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function eqn = poly_equation(a_hat)
eqn = " y = "+a_hat(1);
for i = 2:(length(a_hat))
if sign(a_hat(i))>0
str = " + ";
else
str = " ";
end
if i == 2
eqn = eqn+str+a_hat(i)+"*x";
else
eqn = eqn+str+a_hat(i)+"*x^"+(i-1)+" ";
end
end
eqn = eqn+" ";
end

카테고리

Help CenterFile Exchange에서 Regression에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by