Change initial conditions to maximize Rsquared
4 次查看(过去 30 天)
显示 更早的评论
For example is there a way to tell matlab + or - these varaibles (X,Y,Z) by 0.001 as needed to maxamize the R squared value??
Such as a while loop that looks like this? this is a made up script for an example
X1 = 1
Y1 = 1
Z1 = 1
X2 = 1
Y2 = 1
Z2 = 1
Fit = fitlm(A,B);
R2 = Fit.Rsquared.Ordinary; ( gets an R squared lets assume not 1)
while R2 < 0.8
dx = 0.001 ( I want this dx to be + or - 0.001 somehow in order to maxamize R squared (R2))
X1 = X1 +dx
Y1 = Y1+dz
Z1 = Z1+dx
..... same for X2 Y2 Z2
A = sin(X1+Y1+Z1)
B=sin(X2+Y2+Z2)
Fit = fitlm(A,B);
R2 = Fit.Rsquared.Ordinary;
end
0 个评论
回答(1 个)
Deepak
2025-6-4
I understand that you want to adjust variables (X1, Y1, Z1, X2, Y2, Z2) by small amounts (±0.001) in order to maximize the R-squared (R²) value from a fitlm regression. The goal is to iteratively tweak these variables until R² exceeds a target threshold, such as 0.8.
You can achieve this using a loop that tests small ±0.001 changes and updates the variables when an improvement in R² is found. Below is a sample MATLAB code to achieve the same. This approach iteratively improves R² by testing ±dx steps.
X1=1; Y1=1; Z1=1; X2=1; Y2=1; Z2=1; dx=0.001; bestR2=0;
while bestR2 < 0.8
improved = false;
for s = [-1 1]
A = sin((X1+s*dx)+(Y1+s*dx)+(Z1+s*dx));
B = sin((X2+s*dx)+(Y2+s*dx)+(Z2+s*dx));
R2 = fitlm(A', B').Rsquared.Ordinary;
if R2 > bestR2
[X1,Y1,Z1,X2,Y2,Z2] = deal(X1+s*dx, Y1+s*dx, Z1+s*dx, X2+s*dx, Y2+s*dx, Z2+s*dx);
bestR2 = R2; improved = true; break;
end
end
if ~improved, break; end
end
Please find attached the documentation of function used for reference:
I hope this assists in resolving the issue.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!