# [matlab] vector-valued functions...

This topic is 1924 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all,

I need solving nonlinear least-squares for my prototype app.
I'm referring to the lsqnonlin function:

[url="http://www.mathworks.com/help/optim/ug/lsqnonlin.html"]http://www.mathworks.../lsqnonlin.html[/url]

Currently, I use the following code:
k = 1 : num
opt_r = @(x) orig_r{k}*x(1) - other_r{k}*x(k);

x = ones(1,num);
[r_exp, resnorm] = lsqnonlin(opt_r, x);

in other words, I need considering all the arrays belonging to my orig_r and other_r cell arrays.

Error using *
Too many input arguments.

What would the correct statement be? Edited by binsansballs

##### Share on other sites
I haven't completely understood what you are trying to achieve. What about
[source lang="plain"]opt_r = @(x) orig_r{1:num} * x(1) - other_r{1:num} .* x(1:num);[/source]
To define vector valued functions you should usually either directly define a new vector with the square braces notation or use vector operations on vectors/matrices.

##### Share on other sites
[quote name='apatriarca' timestamp='1349986367' post='4989236']
I haven't completely understood what you are trying to achieve. What about
[source lang="plain"]opt_r = @(x) orig_r{1:num} * x(1) - other_r{1:num} .* x(1:num);[/source]
To define vector valued functions you should usually either directly define a new vector with the square braces notation or use vector operations on vectors/matrices.
[/quote]

The problem still remains (since it is in .* x(1:num)).

Say num = 3.
x would be a vector of size 3, and I'd like to have:

opt_r = @(x) orig_r{2}.^x(1) - other_r{2}.*x(2) + orig_r{3}.^x(1) - other_r{3}.^x(3)

##### Share on other sites
Can you write your equation in a more mathematical notations, i.e. using series or Einstein notation or Matrix notation or.. I'm not sure what are you trying to achieve with that code. For example, why you are not considering orig_r{1} and other_r{1}? Is there any reason or you simply forgot their terms? What's the generalization to other dimensions?

##### Share on other sites
What are the dimensions of orig_r? I.e., how many matrices does this cell array contain?

And what are the dimensions of its contents? I.e., what are the dimensions of orig_r{1}, and are the dimensions of orig_r{2}, orig_r{3}, etc, all the same as this?

Likewise for other_r: What are that cell array's dimensions, and what are the dimensions of the matrices that are its elements?

---

Some tips for debugging:

For now, stop using inline ("@") notation for your function. Just write a function using the standard "function" syntax that does what you want, and break the computation within it into multiple lines. Then, forget about "lsqnonlin" for now, and just try to call your function and get it to return a number. With your function split up, you will be able to set breakpoints and see what is happening. Once you know that your function works, [i]then[/i] try calling lsqnonlin on it.

If you want to use inline function syntax, slowly move more of the computations in your function into a single line, testing at each stage. This will help you to isolate your mistake. When you do have it represented as a single line, then move that line into an inline ("@") function, and test that it works. Finally, try calling lsqnonlin on it. Edited by Emergent

##### Share on other sites
One moment. I think I'm really misunderstanding something about lsqnonlin...

I've got problems even with this simple code:

opt = {3};
for i = 1 : 3
opt{i} = @(x) x^i;
end
[r resnorm] = lsqnonlin(opt,10);

the error is

FUN must be a function or an inline object;
or, FUN may be a cell array that contains these type of objects.

but class(opt) returns me a cell...

##### Share on other sites
Oh! I think I understand why you're doing what you're doing. You've been getting the error message,

[quote]
??? Error using ==> lsqfcnchk at 117
FUN must be a function or an inline object;
or, FUN may be a cell array that contains these type of objects.
[/quote]

so you've been thinking FUN can be a cell array. Then you type "help lsqnonlin," and you read

[quote]
X = LSQNONLIN(FUN,X0) starts at the matrix X0 and finds a minimum X to
the sum of squares of the functions in FUN.
[/quote]

and that phrase "the functions in FUN" leads you to believe that there are [i]multiple[/i] functions.

All this is extremely misleading. FUN is [i]one[/i] function. It [i]returns[/i] a vector. Why the MATLAB documentation confuses the issue, I do not know.

Here's a correct version of what I think you were attempting with your last code:
[code]
[x, resnorm] = lsqnonlin(@residuals, 10);

fprintf('x = %g\nresnorm = %g\n', x, resnorm);
end
function r = residuals(x)
for i=1:3
r(i) = x^i;
end
end
[/code]

Note that the answer is trivial. We're looking for the number "x" that minimizes (x^1)^2 + (x^2)^2 + (x^3)^3, and that number is of course zero. Note also that "residuals" can be written more efficiently than above as,
[code]
function r = residuals(x)
r = x.^[1:3];
end
[/code]
and that, if you really want, you can do everything with the one-liner,
[code]
[x, resnorm] = lsqnonlin(@(x)(x.^[1:3]), 10);
[/code]

Finally, I think you may also be a bit confused about cell arrays. The syntax, "opt = {3};" does not declare a cell array of size 3x1 or 1x3. It creates a cell array of size 1x1, containing the value 3. For an example of what the curly brackets do in this context, consider the statement "opt = {[1;2], eye(2), 1};" this makes a 1x3 cell array containing a 2x1 vector, a 2x2 matrix, and a scalar.

I hope that helps. Edited by Emergent

##### Share on other sites
Thank you, this was exactly what I was misunderstanding!
Unfortunately, in my actual code, I can not use the first solution you suggest (the introduction of a second function would be
problematic). And for the second approach... I tried something similar but it gave me some problems since I had to iterate on
cell arrays, not on simpe arrays:

If x is a cell array, I can not use the syntax x{1:3}...

Anyway, thank you again. On Monday I'll test the function again and give you a feedback!

##### Share on other sites
[quote name='binsansballs' timestamp='1350073772' post='4989573']
Unfortunately, in my actual code, I can not use the first solution you suggest (the introduction of a second function would be
problematic).[/quote]

It may not be necessary, but in case it's helpful, do remember that you can both nest functions, and define multiple functions in the same file.

[quote]
And for the second approach... I tried something similar but it gave me some problems since I had to iterate on
cell arrays, not on simpe arrays:

If x is a cell array, I can not use the syntax x{1:3}...[/quote]

If all the elements of your cell array have the same dimension, "cell2mat" could be an option.