Solving for 2 unknowns with nonlinear regression

2 visualizzazioni (ultimi 30 giorni)
I am having trouble setting up my nonlinear fit because I can't figure out how to write the model equation. I keep getting a parse error. Seems like such a simple fix, but it has been persistent.
Cc=[1, 1.39, 1.93, 2.66 ,3.7 , 7.12, 13.7];
Cs=[250, 245, 238, 229, 216, 197, 94.4];
dC=[.30, .45, .63, .87, 1.21, 2.32, 4.42]';
CC=Cc.*Cs;
x=[CC' Cs'];
model=@(b,x) ( x(:,1).*b(1). )/ ( x(:,2).+ b(2).);
initial=[1,1];
[beta,r,j,covb,mse]=nlinfit(x,dC,model,initial)
is nonlin fit not the right way to go about this?

Risposta accettata

John D'Errico
John D'Errico il 2 Mar 2016
Modificato: John D'Errico il 2 Mar 2016
No. nlinfit is entirely adequate.
However, it would seem that learning to write valid MATLAB syntax would help. It seems that you think it is correct to randomly insert . (dots) everywhere, sprinkling them with wild abandon.
model=@(b,x) ( x(:,1).*b(1). )/ ( x(:,2).+ b(2).);
For example, why have you placed a dot immediately after b(1)? After b(2)? After x(:,1)? After x(:,2)?
I think you misunderstand when to use a dot, and what it does. The . is used in three places, to create element-wise multiplies, divides, and powers.
My guess is that you think for some reason, that you need to have a . after a variable name? Wrong, completely so.
A dot is part of an operator, here .*, ./, and .^, although the last is not used in your code.
help times
help slash
help power
  2 Commenti
andy hendrix
andy hendrix il 2 Mar 2016
Modificato: andy hendrix il 2 Mar 2016
Right so I originally had the models as
model=@(b,x) ( x(:,1).*b(1) )/ ( x(:,2)+ b(2));
But I kept getting a parse error and just tried every combination. It said that i was getting a 7x7 matrix instead of the 7x1 in the Y. I
It is my understanding that the dot makes it so that each row value in the column is multiplied to the other values of that row, instead of just matrix multiplication. Is that correct?
John D'Errico
John D'Errico il 2 Mar 2016
Modificato: John D'Errico il 2 Mar 2016
No. You are not correct in your understanding.
A dot operator allows you to perform element-size multiplication, or division. For example, I wish to multiply every element of the vector a with the corresponding one in b.
a = 1:4;
b = [2 3 5 7];
So now, IF I tried to write it as a*b, MATLAB would get upset at me, because * is the dot product operator, NOT the simple multiplication operator you might expect. Instead, I need to write it as
a.*b
ans =
2 6 15 28
Yes, you CAN multiple two scalars together using a *, since scalar multiples are nothing special. As well, MATLAB expands scalars for *, so we can do this:
5*b
ans =
10 15 25 35
division is the same. the slash operator (/) is NOT just a divide. Yes, it works when dividing by a scalar.
b/2
ans =
1 1.5 2.5 3.5
But a/b will not do what you expect.
a/b
ans =
0.58621
Huh? It turns out that a/b generates the solution to the problem
x*b = a
such that x minimizes the sum of the squares of the residuals. What you might have wanted is a./b, a form that divides each corresponding pair of elements.
a./b
ans =
0.5 0.66667 0.6 0.57143
Again, READ THE HELP! All of this is indeed explained in the help for slash, times, and power.
So what you probably needed to do was simple:
model=@(b,x) ( x(:,1).*b(1) ) ./ (x(:,2) + b(2));
One extra dot, but in the right place. In fact, the .* operator between x(:,1) and b(1) could have been just a simple * since b(1) is a scalar.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by