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

回答(1 个)

Deepak
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 () value from a fitlm regression. The goal is to iteratively tweak these variables until 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 is found. Below is a sample MATLAB code to achieve the same. This approach iteratively improves 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.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by