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

8 vues (au cours des 30 derniers jours)
Rami Altam
Rami Altam le 28 Mar 2018
Commenté : Rami Altam le 28 Mar 2018
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.

Réponses (1)

Walter Roberson
Walter Roberson le 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 commentaires
Walter Roberson
Walter Roberson le 28 Mar 2018
Modifié(e) : Walter Roberson le 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
Rami Altam
Rami Altam le 28 Mar 2018
Thank you

Connectez-vous pour commenter.

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by