MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Jonathan Nguyen
on 29 Dec 2012

I have a exercise find min f(x) Sum(i=1 --> n) (1 - x*(2i-1)^2)^2 + 10[(x*(2i) - [x*(2i-1)]^2]^2 Running in matlab function [ p, num_cg, iflag ] = cg_steihaug ( G, b, delta, params );

n = length(b);

errtol = params(1); maxit = params(2); iprnt = params(3); iflag = ' '; g = b;

x = zeros(n,1);

r = -g;

rho = z'*r;

tst = norm(r);

flag = '';

terminate = errtol*norm(g); it = 1; hatdel = delta*(1-1.d-6);

rhoold = 1.0d0;

if iprnt > 0

fprintf(1,'\n\tThis is an output of the CG-Steihaug method. \n\tDelta = %7.1e \n', delta);

fprintf(1,' ---------------------------------------------\n');

end

flag = 'We do not know ';

if tst <= terminate; flag = 'Small `|g|` '; end

while((tst > terminate) & (it <= maxit) & norm(x) <= hatdel)

if(it == 1)

p = z; else

beta = rho/rhoold;

p = z + beta*p;

end

w = G*p; alpha = w'*p;

If alpha < = 0 head to the TR boundary and return

ineg = 0;

if(alpha <= 0)

ac = p'*p; bc = 2*(x'*p); cc = x'*x - delta*delta;

alpha = (-bc + sqrt(bc*bc - 4*ac*cc))/(2*ac);

flag = 'negative curvature';

iflag = 'NC';

else

alpha = rho/alpha;

if norm(x+alpha*p) > delta

ac = p'*p; bc = 2*(x'*p); cc = x'*x - delta*delta;

alpha = (-bc + sqrt(bc*bc - 4*ac*cc))/(2*ac);

flag = 'boundary was hit';

iflag = 'TR';

end

end

x = x + alpha*p;

r = r - alpha*w;

tst = norm(r);

if tst <= terminate; flag = '||r|| < test '; iflag = 'RS'; end;

if norm(x) >= hatdel; flag = 'close to the boundary'; iflag = 'TR'; end

if iprnt > 0

fprintf(1,' %3i %14.8e %s \n', it, tst, flag);

end

rhoold = rho;

z = r;

rho = z'*r;

it = it + 1;

end

if it > maxit; iflag = 'MX'; end;

num_cg = it;

p = x;

And in command windows:

syms x1 x2

f=(1 - x*(2i-1)^2)^2 + 10[(x*(2i) - [x*(2i-1)]^2]^2

b=jacobian(f);

G=jacobian(jacobian(f));

delta=1;

params=[1,100,10^-3];

[ p, num_cg, iflag ] = cg_steihaug ( G, b, delta, params )

And I can't run. Please, help me !

*No products are associated with this question.*

Answer by Image Analyst
on 29 Dec 2012

Why can't you run it? After you typed in this line

[ p, num_cg, iflag ] = cg_steihaug ( G, b, delta, params )

into the command window, **did you hit the enter key**? It should run. Does it say that it can't find "cg_steihaug"? If so, make sure it's in the current folder or on your search path. Otherwise, if you have some other definition of " **can't run**" then, by all means, please share it with us.

Alternatively you can open an editor window and type all that stuff into an m-file and click the green "run" icon to run the m-file.

Show 1 older comment

Jonathan Nguyen
on 29 Dec 2012

Matlab still report:

f=(1-x*(2i-1)^2)^2+10[x*(2i)-[x*(2i-1)]^2]^2 | Error: Unbalanced or unexpected parenthesis or bracket.

Walter Roberson
on 29 Dec 2012

You still have a '[' directly following your '10'.

f=(1-x*(2i-1)^2)^2+10*(x*(2i)-(x*(2i-1))^2)^2

Answer by Walter Roberson
on 29 Dec 2012

In the command window you used

syms x1 x2

but then you do not refer to x1 or x2 in anything that follows in the command window.

But in the next line you try to define f in terms of x, but x has no definition at that point. I can see that you do *not* want an anonymous function, as you use jacobian(f) which is only defined for symbolic expressions. Perhaps you wanted

syms x

instead of

syms x1 x2

Note though that the symbolic jacobian routine requires that you pass in two parameters, the second one being the variable to take the jacobian with respect to.

After that you pass the jacobians into your cg_steihaug and once there you try to do arithmetic calculations on it and you expect the result to be numeric (something you can compare to alpha). You need to think about that further. Possibly at some point you will want to use subs() to substitute values into the symbolic formula, and then double() to convert the resulting symbolic number into a double-precision number.

Jonathan Nguyen
on 30 Dec 2012

Matlab report: Undefined function 'norm' for input arguments of type 'sym'

Error in cg_steihaug (line 41)

tst = norm(r);

My exercise is

min f(x) Sum(i=1 --> n) (1 - x*(2i-1)^2)^2 + 10*[(x*(2i) - [x*(2i-1)]^2]^2

and I need to run it with n = 10

## 3 Comments

## Walter Roberson (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/57601#comment_119675

http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup

## Jonathan Nguyen (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/57601#comment_119682

Thks !

## Jan Simon (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/57601#comment_119778

Please, Jonathan, follow Walter's link and format the code properly to improve the readability.