How to write a For function using values from a table ?

8 views (last 30 days)
My equation is:
y(1) = FOPT -(55930.4 - 330502.*x(1) - 3.18673E6.*x(2) + 344393.*x(3) + 471232.*x(4) + 1.58821E6.*x(5) - 156.186.*x(6));
I want to create a For function and substitute FOPT of a value from a table in the Matlab named FOPT.
Thank you.

Answers (1)

Walter Roberson
Walter Roberson on 28 Mar 2018
y = NameOfTable.FOPT - (55930.4 - 330502.*x(1) - 3.18673E6.*x(2) + 344393.*x(3) + 471232.*x(4) + 1.58821E6.*x(5) - 156.186.*x(6));
All of the x references are scalars, so the expression in () is a scalar, and a scalar can be subtracted from a matrix without difficulty.
Note: if you are trying to fit parameters against a model, then unless you have strong constraints on your x values, there is not going to be a unique answer, since a change in any one of the x() values can be exactly compensated by a change in another value. For example an increase of 1 in x(1) can be exactly compensated by a decrease of 330502 / 156.186 = 2116.07954618212 in x(6) or an increase of 344393/330502 = 1.04203000284416 in x(3)
  3 Comments
Walter Roberson
Walter Roberson on 28 Mar 2018
Edited: Walter Roberson on 28 Mar 2018
FOPT_values = NameOfTable.FOPT;
num_FOPT = length(FOPT_values);
y = zeros(num_FOPT, 1);
for FOPT_idx = 1 : num_FOPT
FOPT = FOPT_values(FOPT_idx);
y(FOPT_idx) = FOPT -(55930.4 - 330502.*x(1) - 3.18673E6.*x(2) + 344393.*x(3) + 471232.*x(4) + 1.58821E6.*x(5) - 156.186.*x(6));
end
... But this is a waste of time, since you can use the simple code
y = NameOfTable.FOPT - (55930.4 - 330502.*x(1) - 3.18673E6.*x(2) + 344393.*x(3) + 471232.*x(4) + 1.58821E6.*x(5) - 156.186.*x(6));
This does substitute FOPT from the table named NameOfTable

Sign in to comment.

Categories

Find more on Tables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!