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

Thread Subject:
Matlab Problem!!!

Subject: Matlab Problem!!!

From: Matt

Date: 3 Dec, 2008 04:23:30

Message: 1 of 10

I'm trying to run the program:

function [x,numIts]=newton(f,x)
EPSILON = 1.0e-6;
MAXITS = 500;

for numIts=1:MAXITS
  [y,yprime] = feval(f,x);
  increment = y\yprime;
  x = x - increment;

  if norm(increment)<EPSILON
    break;
  end
end

But cannot seem to get it to run. What commands do I need
to enter to get this thing to run?!?!?! Thank you for the
help

Subject: Matlab Problem!!!

From: Walter Roberson

Date: 3 Dec, 2008 04:44:16

Message: 2 of 10

Matt wrote:
> I'm trying to run the program:

> function [x,numIts]=newton(f,x)
[etc]

> But cannot seem to get it to run. What commands do I need
> to enter to get this thing to run?!?!?!

What, more precisely, have you tried so far?

Are you familiar with construction .m files? Are you familiar with
how to call functions in matlab? What parameters are you passing in
when you call it?

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: Matlab Problem!!!

From: Matt

Date: 3 Dec, 2008 05:08:30

Message: 3 of 10

I've tried many things. I already have it saved as newton.m file. And I'm trying multiple things in the M-File Configuration Box. I've tried:

x = 1;
f = [@(x)x.^2, @(x)2*x];
newton(f, 0.2);

f = 'x^2';
newton(f, 0.2);

newton('x^2', 0.2);

And all of them give crazy errors :(

Subject: Matlab Problem!!!

From: Roger Stafford

Date: 3 Dec, 2008 06:40:19

Message: 4 of 10

Matt <mcushi2@gmail.com> wrote in message <6200318.1228278241565.JavaMail.jakarta@nitrogen.mathforum.org>...
> I'm trying to run the program:
>
> function [x,numIts]=newton(f,x)
> EPSILON = 1.0e-6;
> MAXITS = 500;
>
> for numIts=1:MAXITS
> [y,yprime] = feval(f,x);
> increment = y\yprime;
> x = x - increment;
>
> if norm(increment)<EPSILON
> break;
> end
> end
>
> But cannot seem to get it to run. What commands do I need
> to enter to get this thing to run?!?!?! Thank you for the
> help

Matt <mcushi2@gmail.com> wrote in message <23500797.1228280941124.JavaMail.jakarta@nitrogen.mathforum.org>...
> I've tried many things. I already have it saved as newton.m file. And I'm trying multiple things in the M-File Configuration Box. I've tried:
>
> x = 1;
> f = [@(x)x.^2, @(x)2*x];
> newton(f, 0.2);
>
> f = 'x^2';
> newton(f, 0.2);
>
> newton('x^2', 0.2);
>
> And all of them give crazy errors :(
----------
  The "increment = y\yprime" line is wrong. It should be a forward slash. As it stands, you are dividing the derivative by the function value instead of visa versa.

  Also, if x is intended to be a scalar quantity, and therefore 'increment' also a scalar, why do you say "norm(increment)" instead of just plain "abs(increment)"? It will still give the correct answer but is misleading to anyone who reads the code.

  As to the three attempts to call 'newton' in your second article, the second two tries are bound to fail since they don't return the desired derivative. 'feval' is certainly not smart enough to furnish a derivative of its own accord. Also what was the point in writing "x = 1;" there?

Roger Stafford

Subject: Matlab Problem!!!

From: Bruno Luong

Date: 3 Dec, 2008 07:33:05

Message: 5 of 10

Matt <mcushi2@gmail.com> wrote in message <23500797.1228280941124.JavaMail.jakarta@nitrogen.mathforum.org>...

>
> And all of them give crazy errors :(

You gain a lot by learning how to use the debugger to trackdown what could be wrong in the code.

Bruno

Subject: Matlab Problem!!!

From: Rodney Thomson

Date: 3 Dec, 2008 07:41:04

Message: 6 of 10

Matt <mcushi2@gmail.com> wrote in message <6200318.1228278241565.JavaMail.jakarta@nitrogen.mathforum.org>...
> I'm trying to run the program:
>
> function [x,numIts]=newton(f,x)
> EPSILON = 1.0e-6;
> MAXITS = 500;
>
> for numIts=1:MAXITS
> [y,yprime] = feval(f,x);
> increment = y\yprime;
> x = x - increment;
>
> if norm(increment)<EPSILON
> break;
> end
> end
>
> But cannot seem to get it to run. What commands do I need
> to enter to get this thing to run?!?!?! Thank you for the
> help

Look up the help for feval as it DOES NOT DO WHAT YOU ARE TRYING IT TO DO!

It executes a MATLAB function, not a generic mathematic function. You need to wrap your mathematical function in a MATLAB function first:

>> eqtn = @(x)(x^2)
eqtn =
    @(x)(x^2)
>> feval(eqtn, 4)
ans =
    16

Rod

--
http://iheartmatlab.blogspot.com

Subject: Matlab Problem!!!

From: Bjorn Gustavsson

Date: 3 Dec, 2008 08:51:03

Message: 7 of 10

Matt <mcushi2@gmail.com> wrote in message <23500797.1228280941124.JavaMail.jakarta@nitrogen.mathforum.org>...
> I've tried many things. I already have it saved as newton.m file. And I'm trying multiple things in the M-File Configuration Box. I've tried:
>
> x = 1;
> f = [@(x)x.^2, @(x)2*x];
> newton(f, 0.2);
>
In my current version of matlab (7.1) the above pattern doesn't work, since you'd get a call along this line somewhere:

val = f(1)(12)

As far as I recall the recommendation with arrays of function handles are to use cell arrays:

f = {@(x) (x.^2), @(x) (2*x)};

Then the call will be something like this:
fv = f{1}(12);
dfdx = f{2}(12);

The problem is that matlab is designed with multiple usage of ().

HTH,
Bjorn

Subject: Matlab Problem!!!

From: Steven Lord

Date: 3 Dec, 2008 14:44:57

Message: 8 of 10


"Bjorn Gustavsson" <bjonr@irf.se> wrote in message
news:gh5h9n$m1o$1@fred.mathworks.com...
> Matt <mcushi2@gmail.com> wrote in message
> <23500797.1228280941124.JavaMail.jakarta@nitrogen.mathforum.org>...
>> I've tried many things. I already have it saved as newton.m file. And
>> I'm trying multiple things in the M-File Configuration Box. I've tried:
>>
>> x = 1;
>> f = [@(x)x.^2, @(x)2*x];
>> newton(f, 0.2);
>>
> In my current version of matlab (7.1) the above pattern doesn't work,
> since you'd get a call along this line somewhere:
>
> val = f(1)(12)
>
> As far as I recall the recommendation with arrays of function handles are
> to use cell arrays:
>
> f = {@(x) (x.^2), @(x) (2*x)};

It depends on what the OP wants. Rather than a cell array of function
handles, I suspect he may want a function handle that returns an array:

f = @(x) [x.^2; 2*x];

Then something like f(5) would work.

--
Steve Lord
slord@mathworks.com

Subject: Matlab Problem!!!

From: Roger Stafford

Date: 3 Dec, 2008 17:14:02

Message: 9 of 10

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gh59kj$hrr$1@fred.mathworks.com>...
> Matt <mcushi2@gmail.com> wrote in message <6200318.1228278241565.JavaMail.jakarta@nitrogen.mathforum.org>...
> > ......
> > f = [@(x)x.^2, @(x)2*x];
> > newton(f, 0.2);
> > .......
> .....
> As to the three attempts to call 'newton' in your second article, the second two tries are bound to fail since they don't return the desired derivative. 'feval' is certainly not smart enough to furnish a derivative of its own accord.
> ......

  I would also like to point out a theoretical difficulty with Newton's method that occurs with the type of function you gave as an example, Matt. In that case the root of the equation f(x) = x^2 = 0 occurs at a point where the derivative also has a root, f'(x) = 0. When this happens there is danger of the ratio f(x(n))/f'(x(n)) becoming indeterminate, which is to say, capable of serious computational errors in obtaining x(n+1) from x(n).

  For example if you had used f(x) = x^2-2*pi+pi^2, with a root at x = pi, where its derivative also has a root, you would not be able to find that root to better than about eight-place accuracy using Newton's method, in spite of Matlab's general sixteen-place accuracy.

Roger Stafford

Subject: Matlab Problem!!!

From: Bjorn Gustavsson

Date: 4 Dec, 2008 08:12:06

Message: 10 of 10

"Steven Lord" <slord@mathworks.com> wrote in message <gh6619$npo$1@fred.mathworks.com>...
>
> "Bjorn Gustavsson" <bjonr@irf.se> wrote in message
> news:gh5h9n$m1o$1@fred.mathworks.com...
> > Matt <mcushi2@gmail.com> wrote in message
> > <23500797.1228280941124.JavaMail.jakarta@nitrogen.mathforum.org>...
> >> I've tried many things. I already have it saved as newton.m file. And
> >> I'm trying multiple things in the M-File Configuration Box. I've tried:
> >>
> >> x = 1;
> >> f = [@(x)x.^2, @(x)2*x];
> >> newton(f, 0.2);
> >>
> > In my current version of matlab (7.1) the above pattern doesn't work,
> > since you'd get a call along this line somewhere:
> >
> > val = f(1)(12)
> >
> > As far as I recall the recommendation with arrays of function handles are
> > to use cell arrays:
> >
> > f = {@(x) (x.^2), @(x) (2*x)};
>
> It depends on what the OP wants. Rather than a cell array of function
> handles, I suspect he may want a function handle that returns an array:
>
> f = @(x) [x.^2; 2*x];
>
> Then something like f(5) would work.
>
Is there any neat way to make that vectorize well too - so that one get an N-by-2 array out if called with an N-by-1 and a 2-by-N when called with a 1-by-N?

Bjoern, trying to learn something - sometimes

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us