COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

Newton Raphson for nxn system using symbolic toolbox

• Email
• Follow

```Hello

I have already implemented the Newton Raphson method for a
system of n equations. However in my implementation I have
to give the function and its Jacobian (written as a
function) as a input variable.

Now I want to use the symbolic toolbox in order not to
calculate the Jacobian myself. My basic problem is later to
evaluate that expression, since I don't know before hand the
size of the system. While this is not a problem for an
"ordinary" function which uses vectors it seems to be a
problem for symbolic expression.

Here is an example trying to use vectors
clear all
syms x y
e=[x y]
f1=sin(e(1)-e(2))
e(1)=1
e(2)=-1
eval(f1)

But this does not work! It  gives sin(x+y)

While of course
x=1
y=-1
eval(f1)
works and  gives
f1= 0.9093
but of course for a nxn system I don't know before hand the
size of the system and how many variables I have to set to
numerical values before evaluating the expression f1.

Can anybody help me?

thanks

Uwe Brauer
```
 0
Reply oub (94) 11/1/2011 10:20:29 AM

See related articles to this posting

```On 11/1/2011 6:20 AM, Uwe Brauer wrote:
> Hello
>
> I have already implemented the Newton Raphson method for a
> system of n equations. However in my implementation I have
> to give the function and its Jacobian (written as a
> function) as a input variable.
>
> Now I want to use the symbolic toolbox in order not to
> calculate the Jacobian myself. My basic problem is later to
> evaluate that expression, since I don't know before hand the
> size of the system. While this is not a problem for an
> "ordinary" function which uses vectors it seems to be a
> problem for symbolic expression.
>
>
> Here is an example trying to use vectors
> clear all
> syms x y e=[x y]
> f1=sin(e(1)-e(2)) e(1)=1
> e(2)=-1
> eval(f1)
>
> But this does not work! It gives sin(x+y)
>
> While of course
> x=1
> y=-1
> eval(f1)
> works and gives f1= 0.9093
> but of course for a nxn system I don't know before hand the
> size of the system and how many variables I have to set to
> numerical values before evaluating the expression f1.
>
> Can anybody help me?
>
> thanks
>
> Uwe Brauer

Perhaps this Optimization Toolbox documentation example will help:
http://www.mathworks.com/help/toolbox/optim/ug/brn4nh7.html#brv_i_1

Alan Weiss
MATLAB mathematical toolbox documentation
```
 0
Reply aweiss (880) 11/1/2011 3:17:07 PM

```On 01.11.11 11:20, Uwe Brauer wrote:

> Here is an example trying to use vectors
>  clear all
>  syms x y
>  e=[x y]
>  f1=sin(e(1)-e(2))
>  e(1)=1
>  e(2)=-1
>  eval(f1)

subs(f1, e, [1 -1])

Or, as Alan pointed out, convert f1 to a MATLAB function. If you have
multiple evaluations of the same symbolic expression, that will usually
be faster.

Christopher
```
 0

```Christopher Creutzig <Christopher.Creutzig@mathworks.com> wrote in message <4EBA8B5A.5090609@mathworks.com>...
> On 01.11.11 11:20, Uwe Brauer wrote:
>
> > Here is an example trying to use vectors
> >  clear all
> >  syms x y
> >  e=[x y]
> >  f1=sin(e(1)-e(2))
> >  e(1)=1
> >  e(2)=-1
> >  eval(f1)
>
>
> subs(f1, e, [1 -1])
>
> Or, as Alan pointed out, convert f1 to a MATLAB function. If you have
> multiple evaluations of the same symbolic expression, that will usually
> be faster.
>
>
> Christopher

unfortunately my Matlab version does not provide this feature
for symbolic expressions

But my main problem remains.

I can perfectly write matlab code for solving Newton Raphson for any nxn system, since Matlab uses dynamic memory,
and I have to use as a entry variable the system I want to solve and its jacobian.

However when i only want to have the system and use Matlabs symbolic tool to calculate the jacobian I do not know how to do that since I don't know the size of the jacobian in advance.

Uwe
```
 0
Reply oub (94) 11/11/2011 3:17:21 PM

```On 11.11.11 16:17, Uwe Brauer wrote:

> I can perfectly write matlab code for solving Newton Raphson for any nxn system, since Matlab uses dynamic memory,
> and I have to use as a entry variable the system I want to solve and its jacobian.
>
> However when i only want to have the system and use Matlabs symbolic tool to calculate the jacobian I do not know how to do that since I don't know the size of the jacobian in advance.

I still don't understand the problem. Something like the following
sketch should work, no?

....
J = jacobian(expression, vars);
....
% within some loop:
...
Fval = double(subs(expression, vars, values));
Jac  = double(subs(J, vars, values));
...

Christopher
```
 0

```Christopher Creutzig <Christopher.Creutzig@mathworks.com> wrote in message <4EC235EE.9040108@mathworks.com>...
> On 11.11.11 16:17, Uwe Brauer wrote:
>
> > I can perfectly write matlab code for solving Newton Raphson for any nxn system, since Matlab uses dynamic memory,
> > and I have to use as a entry variable the system I want to solve and its jacobian.
> >
> > However when i only want to have the system and use Matlabs symbolic tool to calculate the jacobian I do not know how to do that since I don't know the size of the jacobian in advance.
>
> I still don't understand the problem. Something like the following
> sketch should work, no?
>
> ...
> J = jacobian(expression, vars);
> ...
> % within some loop:
>   ...
>   Fval = double(subs(expression, vars, values));
>   Jac  = double(subs(J, vars, values));
>   ...
>
>
>
> Christopher

right, after thinking about it I came up with a similar solution,
BTW  why do you use double?

function xvec=newtoniteratesymb(f,e,x0,TOL,nmax)

n=x0;
fev=subs(f,e,n);
jac=jacobian(f,e);
jacev=subs(jac,e,n);
xvec=x0;
k=1;
diff=100;

while diff>TOL & k<nmax
fev=subs(f,e,xvec(:,k));
jacev=subs(jac,e,xvec(:,k));
xvec(:,k+1)=xvec(:,k)- jacev\fev;
diff=max(max(xvec(:,k+1)-xvec(:,k)));
k=k+1;
end

thanks

Uwe
```
 0
Reply oub (94) 11/15/2011 10:15:26 AM

```"Uwe Brauer" wrote in message <j9te3u\$cqg\$1@newscl01ah.mathworks.com>...
> Christopher Creutzig <Christopher.Creutzig@mathworks.com> wrote in message <4EC235EE.9040108@mathworks.com>...
> > On 11.11.11 16:17, Uwe Brauer wrote:
> >
> > > I can perfectly write matlab code for solving Newton Raphson for any nxn system, since Matlab uses dynamic memory,
> > > and I have to use as a entry variable the system I want to solve and its jacobian.
> > >
> > > However when i only want to have the system and use Matlabs symbolic tool to calculate the jacobian I do not know how to do that since I don't know the size of the jacobian in advance.
> >
> > I still don't understand the problem. Something like the following
> > sketch should work, no?
> >
> > ...
> > J = jacobian(expression, vars);
> > ...
> > % within some loop:
> >   ...
> >   Fval = double(subs(expression, vars, values));
> >   Jac  = double(subs(J, vars, values));
> >   ...
> >
> >
> >
> > Christopher
>
> right, after thinking about it I came up with a similar solution,
> BTW  why do you use double?
>
> function xvec=newtoniteratesymb(f,e,x0,TOL,nmax)
>
> n=x0;
> fev=subs(f,e,n);
> jac=jacobian(f,e);
> jacev=subs(jac,e,n);
> xvec=x0;
> k=1;
> diff=100;
>
> while diff>TOL & k<nmax
>   fev=subs(f,e,xvec(:,k));
>   jacev=subs(jac,e,xvec(:,k));
>   xvec(:,k+1)=xvec(:,k)- jacev\fev;
>   diff=max(max(xvec(:,k+1)-xvec(:,k)));
>   k=k+1;
> end
>
> thanks
>
> Uwe

Hello, I head a similar problem some days ago and i have to say this method works perfectly for small systems but when you go after bigger problems you might find some problems while using the function jacobian(f) and computing it's value every iteration. This is very demending on memory and CPU.
```
 0
Reply mirceaa_ (4) 11/15/2011 9:41:10 PM

```On 15.11.11 11:15, Uwe Brauer wrote:
> BTW  why do you use double?

To make sure the result is not a sym object. Often, subs will do that
when substituting double variables, but only if evaluation within MATLAB
works – in which case the double command does no harm either. If the
substitution needs to go through the symbolic toolbox engine, the result
will be a sym object:

>> syms x
>> subs(erf(i*x), x, 1)

ans =

erf(i)

>> double(subs(erf(i*x), x, 1))

ans =

0 + 1.650425758797543i

Christopher
```
 0

7 Replies
45 Views

Similar Articles

12/12/2013 2:38:55 AM
[PageSpeed]

Similar Artilces:

Newton Raphson method for chemical equilibrium system
I am trying to solve a system of equations relating to a chemical equilibrium problem in the form or A*x = B, five equations and five unknowns. The A-matrix is 5x5 as is the Jacobian. I have no problem solving the current system; however, when I try to solve for a larger system I start running into problems. I was wondering if anyone could provide some suggestions on how to use this method for a larger system of equations, thus solving for more unknowns. My function for the Newton Raphson method is shown below. Please let me know how I could improve the code. It currently works well even...

Looking for tips on using toolbox, smart fasteners with PLM system.
We need to modify SolidWorks Toolbox fasteners in such a way as to: 1. Have a single unchanging part (such as socket head cap screw) that can be placed into our PLM as a "read only part" covering all our company's standard sizes for that type of fastener. (Currently our trouble is that we see configurations being added on the fly) 2. Have the fasteners work correctly with Smart Fasteners, and also with the fastener toolbox. 3. Have our company's standard part numbers and descriptions show up correctly on the BOM. 4. Have the SolidWorks design tree show a reasonable name and ...

estimate states of state space moddel using system identification toolbox
After the state space model is trained use pem in system identification toolbox, how to estimate the states with the data. The predict function can only be used to get observed value. Thank for your help ...

Using template in safety-critical system (flight critical system)
Hi. I want to know whether I can use template in safety-critical system or not. From googling I fount below from http://gotw.ca/sands3.htm. and I was astonished at "You must use template". 1:30-2:45pm C++ In Safety-Critical Systems (Bjarne Stroustrup) C++ is widely used in embedded systems programming and even in safety- critical and hard-real-time systems. This presentation discusses how to write code in these highly demanding application areas. First the mapping of C++ code to hardware resources is reviewed and the basics abstraction mechanisms (classes and templates) are revi...

Symbolic toolbox limitation?
Take the simple expression: f = sym('x*h(x^2)'); Differentiate it: df = diff(f) df = h(x^2)+2*x^2*D(h)(x^2) Differentiate it again: ddf = diff(df) ddf = 6*D(h)(x^2)*x+4*x^3*@@(D,2)(h)(x^2) And simplify: simplify(ddf) ??? Error using ==> sym/maple at offset 32, `@@` unexpected Error in ==> C:\MATLAB6p5p1\toolbox\symbolic\@sym\simplify.m On line 14 ==> r = maple('map','simplify',s); Not the desired result. Any suggestions? -Aj In article <bnpcuo\$lmq2@cui1.lmms.lmco.com>, AJ \"no z\" Johnson <aj.jozhnson@lmco.com> wrote: >Ta...

Using symbols in pxfonts
Hi, I want to use the \lJoin symbol from pxfonts in one of my equations. However, I can only retrieve this symbol by adding "\usepackage{pxfonts}" to my preamble. The downside of this is that all of my equations change style which is unwanted. Is there any way of using a symbol (in this case \lJoin) from pxfonts while keeping my original style? Or is it possible to locate \lJoin (and other join symbols) in standard packages? Thanks, Niels "zork" <niels.arnthjensen@gmail.com> writes: > I want to use the \lJoin symbol from pxfonts in one of my equations. >...

Using Basalt with the Toolbox
I sent this earlier today, but I may have mucked up the address, and I cannot see it. My apologies if it has appeared to everyone else. ----------------------------------------------------------------------- The recent discussion in the ROOL discussion groups about AppBasic has prompted me to write a document about the Toolbox features of Basalt, which have never been very well described, if at all. This is online at: http://www.kappa.me.uk/basalttb.htm and it is included as a download with BasaltApp, a prototype application to develop, as: http://www.kappa.me.uk/Examples/swBasalt...

Which file system to use
Hi, Since I might have to setup up the machine that I am having this "weird file name problem" with anyways. I wonder which would be the best file system to use. I tried a search on the internet, but the reviews I found were rather old and an article stating don't export reiserfs by nfs made me worry a little bit. The scenario is following: Dual processor Pentium III, 3 hardware RAIDs (up to 2 TB), some of those get exported by NFS and Samba. The data stored is very heterogenous. It's hard to tell what the typical file sizes are. The most important issue is reliability. Wh...

newton raphson method
does anyone have any m files for newton raphson method? Thanks a crimson Here's an old script that I wrote for computing eigenvalues in a finite potential square quantum well. Also check out my bessel zeros script in the file exchange (that uses Halley's method) function lambda=newtonwell2(l) k=sqrt(V0-l); iter=20; for n=1:iter [fe,fo,fep,fop]=f(k); K=k-fe/fep; k=K; end lambda=-k^2+V0; function [fe,fo,fep,fop]=f(k) b=sqrt(100-k^2); S=sin(b); C=cos(b); fe=b*S-k*C; fo=b*C+k*S; fep = -(1+k)*(C+k*S/b); fop = (1+k)*(S-k*C/b); boy wrote: > > > does...

Symbols used in classes.
Hi all., Probably an old quesion, but im pretty new to php, and have not had great success in searching. Ive looking over some code which contains the following terms \$this -> :: And have not clue as to what they mean. I have been looking at the php documentation for classes and they appear there, but they are not well explained. Can someone point me a good resource for explaining these, or post me some explaination.s Many thanks. *** Gerry escribi�/wrote (Thu, 31 Jul 2008 08:58:06 -0700 (PDT)): > Probably an old quesion, but im pretty new to php, and have not had > great success...

A plugin system using extend
Hi. I tried to design a simple plugin system using :extend called upon instance (http://ruby-doc.org/core/classes/Object.html#M000335 usual documented behavior), so as to inject some methods redefinitions while keeping the ability to call super in order to fall back on the default behavior if needed. It looks like this: http://gist.github.com/339025 It works as expected, as one can see by reading the commented output at the end of the file: the inheritance chain is altered, with the plugin "subclassing" the original class of the plugin receiver. Yet, I tried to introd...

hi, friends. I am now developing a mobile agent system, the languages I used are Tcl(for mobile) and Prolog(for intelligence). One usage of this system is that you can use agent to search the remote device, and use prolog to do some analysis work. For example, you want to send your friend a gift(CD, or DVDs, or Books), you can use agent to search the music files and books in your friend's device, and get the music names or book names, then use the webservice of amazon to get the author of the music or book, and then find out the other musics or books belong to the same author, and choose o...

Downsampling using System Generator
Hello I am trying to downsample my data. I want to achieve the following. Say my data is 10 samples long [x y x y x y x y x y]. I want to downsample once by 2 starting at the first sample (so that I extract [x x x x x]. I then want to downsample the initial data again, but this time starting at the second data point (so that I extract [y y y y y]). Does anyone know how I can do this? Maybe I use a delay block or figure out how to throw away the first data point, so that my data effectively starts on y? ...

cooling system using LabVIEW
I use this VI to control a cooling system, using sensor (thermistor),&nbsp;&nbsp; lamp, relay&nbsp; and a dc motor. I measure the thermistor temperature and set a threshold to turn on and off the lamp and the motor. is there a better way to do it? &nbsp; temp_control[1].vi: http://forums.ni.com/attachments/ni/170/214877/1/temp_control[1].vi ...

Using the X window system
I installed MINIX 3 onto my system, and installed all of the packages using 'packman' However when i run 'xdm' it gives me a error message. unix wrote: > I installed MINIX 3 onto my system, and installed all of the packages > using 'packman' However when i run 'xdm' it gives me a error message. Try 'startx' or 'xinit' Various nixes use various commands to get X going. Barnacle Bill the Sailor <Barneybill@popeyesnavy.net> wrote: > unix wrote: >> I installed MINIX 3 onto my system, and installed all of the packages >>...

How to use anfis toolbox to do classification
Hello, Can anyone give me an example on how to use anfis to do classification I consulted the help of Matlab, I found a regression example but not classification... ...

Transfering control when using system()?
Hi, I'm writing a GUI interface for a program that executes from the command line in Unix. I've used system("command..") to do it, and it all works fine. However, the problem is that the ideal behaviour would be for the GUI to disappear once the program starts to run. I know that all system() does is call up the command procesor and execute the command before passing control back to the origin program (in my case the GUI), but I need the GUI to pass control completely to the program and just exit so that closing the GUI won't close the program as well. Is this possible w...

Shell used by os.system
Is there a way of specifying the shell in which os.system commands are executed[1]? Is there a way of specifying what startup scripts should be read? [1] On systems where this makes sense, of course. Jacek Generowicz <jacek.generowicz@cern.ch> writes: > Is there a way of specifying the shell in which os.system commands are > executed[1]? Is there a way of specifying what startup scripts should > be read? It uses the underlying system() function, and that calls /bin/sh -c ... on Unix systems. -- Brian Gough Network Theory Ltd, Publishing the Python Manuals --- http://www...

How to localize a symbol when using Manipulate?
Mathematica experts: I came up against this problem when writing a demo. I simplified to it to the following: I want to make a call to a lower level function which makes an expression (say build a polynomial in s) and returns it back to be displayed. I want this function to use a symbol, say s, to build the polynomial. The problem is how to localize the symbol itself so it is not global. Since one dose not declare things in Mathematica, (sometime I wish I could) I can't declare the s to be a symbol, but must just use it. Here is a simple example to show the probl...

Using pdepe for large systems
I'm trying to use pdepe on Matlab version 6.5 (and 7.0) to solve a system of 25 coupled parabolic PDEs (they fit the pdepe help file description of the type of equations that it can solve). I get the following error message : Warning: Failure at t=2.353673e-002. Unable to meet integration tolerances without reducing the step size below the smallest value allowed (8.361926e-017) at time t. (Type "warning off MATLAB:ode15s:IntegrationTolNotMet" to suppress this warning.) > In C:\MATLAB6p5\toolbox\matlab\funfun\ode15s.m at line 743 In C:\MATLAB6p5\toolbox\matlab\funfun\pdepe.m...