MATLAB Answers


How to run this code ???

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');


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;
        beta = rho/rhoold;
        p = z + beta*p;
    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';
        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';
    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);
    rhoold = rho;
    z   = r;
    rho = z'*r;
    it  = it + 1;


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





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

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


Jan Simon
on 30 Dec 2012

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


No tags are associated with this question.


No products are associated with this question.

2 Answers

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.


10 cannot be followed directly by [

Matlab still report:

Error: Unbalanced or unexpected parenthesis or bracket.

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


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.

  1 Comment

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

Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!