my matrix is not square, although i think it is?

I have set the following least squares method up quickly. I know i am using a nonlinear technique to solve a linear problem, however i just wanted to get it working, before i choose a linear method but aparently my matrices are not square:
i = [1 2 3 4 5];
ti = [2 5 7 11 14];
Estimates3=fminsearch(@functionnumbers,1,options,T0,theta_f);
and the function code
function fnumbers = functionnumbers(params3,i,ti)
k = params3;
v0 = 1 + (k/2)*ti(1)^2/ti(1)
fnumbers = sum(ti.^2*(i/ti-(v0-(k/2)*ti))^2);
cheers

댓글 수: 3

What are T0,options,theta_f for this example?
Make sure you're deleting old values too!
Alex
Alex 2011년 8월 8일
Sorry i posted that line incorrectly should have been
Estimates3=fminsearch(@functionnumbers,1,options,i,ti);
in terms of deleting old values, where are you referring to?

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2011년 8월 8일

0 개 추천

fminsearch() accepts at most 3 inputs, and the function passed to it must accept a single input. See http://www.mathworks.com/help/techdoc/ref/fminsearch.html
For information on how to do this properly, please read this

댓글 수: 5

Alex
Alex 2011년 8월 9일
sorry im not sure what the problem is, I am sending fminsearch i, ti, and an initial conditon for the single unknown parameter, i dont think i am sending it too much information? the problem it says is that a matrix is not square?
is it to do with both input and output are column vectors? do i need to transpose something?
It's not that fminsearch is getting too many variables, it's that functionnumbers is getting too many variables!
fminsearch is getting too many variables as well. Look at the documentation. There is no possibility listed there to pass in anything to fminsearch after the "options" structure. Look down to Example 2, where it specifically says that additional variables _cannot_ be passed directly through fminsearch.
If *somehow* the "i" and "ti" reaching your functionnumbers function are the same as the ones you initialized, then notice that they are both vectors rather than scalars, so your expression i/ti-(v0-(k/2)*ti) would calculate a vector result. You then try to use ^2 on that vector, and ^ is the *matrix squaring* operation, the vector matrix-multiplied by itself. You should expect that the code should promptly complain that a matrix (or vector) can only be matrix-multiplied by itself if the matrix is square. Sound familiar?
Alex
Alex 2011년 8월 9일
So i'm not sure what the problem is in terms of "too many variables" but I have used this function before and passed it variables after the options before and it worked fine, at the moment when i run it i get the functionnumbers to display i and ti to make sure it recevies them and it does.
i have adjust the code. V0 does not need a matrix operator i dont think however fnumbers needs too:
v0 = (1 + (k/2)*ti(1)^2/ti(1))
fnumbers = sum(ti.^2*(i/ti-(v0-(k/2)*ti)).^2)
the error changes now from matrix must be square to inner matrix dimensions must agree. I tried transposing ti incase the problem was a pre multiplacation issue (matrix multiplication) but its not the case.
Are you truly wanting to do a _matrix division_ between i and ti ?? If not then you should be using ./ instead of /
Are you truly wanting to do a matrix multiplication between ti.^2 and the rest of the expression? If not, then you should be using .* instead of *

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Alex
Alex 2011년 8월 9일

0 개 추천

So I have got it to run, I'm not sure why but breaking the equation into more equations seemed to work:
[code]function fnumbers = functionnumbers(k,i,ti)
v0 = (1 + (k/2)*ti(1)^2/ti(1)); Fitted_Curve = (v0*ti-(k/2)*ti.^2); Error_Vector = i - Fitted_Curve; fnumbers = sum(Error_Vector.^2);[/code]
calling it from
i = [1 2 3 4 5 6]; ti = [2.503 5.159 7.899 10.883 13.814 17.081];
Estimates3=fminsearch(@functionnumbers,0.004,options,i,ti)
however this isnt the most complicated problem, is there a better way to solve this because the value of k i am expecting for this data is about 0.03 but im getting 0.1 the equations non-linear because of the square, however is fminsearch overkill for this?

댓글 수: 2

In the original equation you had "i" divided by "ti", but in the new equations that division appears to have vanished. It does not appear to me that your old and new equations are equivalent, but it could be that I missed something.
Alex
Alex 2011년 8월 10일
Actually I missed something, Walter's answer was right. Thanks

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by