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

8 次查看(过去 30 天)
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.

回答(1 个)

Walter Roberson
Walter Roberson 2018-3-28
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 个评论
Walter Roberson
Walter Roberson 2018-3-28
编辑:Walter Roberson 2018-3-28
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by