• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
binsansballs

[matlab] vector-valued functions...

8 posts in this topic

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.

I receive the following error:
Error using *
Too many input arguments.

What would the correct statement be? Edited by binsansballs
0

Share this post


Link to post
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.
0

Share this post


Link to post
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)
0

Share this post


Link to post
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?
0

Share this post


Link to post
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
0

Share this post


Link to post
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...
0

Share this post


Link to post
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]
% answer.m
function answer()
[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
1

Share this post


Link to post
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!
0

Share this post


Link to post
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0