Clear Filters
Clear Filters

How do I store these values into my matrix?

7 views (last 30 days)
Quincy Houk
Quincy Houk on 28 Oct 2022
Answered: Eshan Patel on 31 Oct 2022
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?

Answers (1)

Eshan Patel
Eshan Patel on 31 Oct 2022
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.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!