How do I store these values into my matrix?
3 次查看(过去 30 天)
显示 更早的评论
Hello, I am doing an assignment that requires me to create a for loop that iterates through and stores the resulting two values of b0 and b1 into an empty matrix I created. The values I am storing in the matrix follow a formula of ET = b0 (x) ^b1 and I need to subtract this formula from preexisting data points on a graph, square them, sum the values and finally store that sum value in my matrix in hopes of finding the smallest sum. This is my current code. Any help on how I can achieve my goal?
0 个评论
回答(1 个)
Eshan Patel
2022-10-31
Hey Quincy,
I understand that you would like to store the "residualSum" values for combinations of "b0" and "b1" in a matrix and then determine the minimum value for "residualSum". For this purpose, you could use the following loop:
requiredMatrix = zeros(900, 3);
k = 1;
for i = 1:30
temp_b1 = b1;
for j = 1:30
ET = b0*(x).^(temp_b1);
residualData = (y - ET).^2;
residualSum = sum(residualData);
requiredMatrix(k, 1) = b0;
requiredMatrix(k, 2) = temp_b1;
requiredMatrix(k, 3) = residualSum;
temp_b1 = temp_b1 - 0.1;
k = k+1;
end
b0 = b0+0.1;
end
This will get you a matrix with 3 columns, storing the values for "b0", "b1" and "residualSum" respectively.
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!