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

### Problem with using fsolve to solve nonlinear equations

• Email
• Follow

```Hello everybody,

I'm trying to solve a set of nonlinear equations using fsolve. But I have a problem. My script looks similar to this:
......
......First part is quite long and not very interesting. I calculate vector F, which is needed to use in fsolve. After calculations, I have, saved as a char:
aaa = 'F= [x(1)*x(2)-2; x(1) - x(2)*x(1) - 2];'

This is very simple case, normally I have 64 equations with x from x(1) to x(64).
where aaa is a char.
Then I use fprintf to write this to file:

fid = fopen('function.m', 'w');
fprintf(fid,'function F = funkcja(x)\n');
fprintf(fid,'%s',aaa);
fclose(fid);

It works good, when I open this file (function.m) on hard disk it looks exactly like it supposed to look:
function F = function(x)
F= [x(1)*x(2)-2; x(1) - x(2)*x(1) - 2];

Then I define x0 to use in fsolve:
x0 = ones(2,1);
And I try to use fsolve:
[x1,x2] = fsolve(@function,x0);

I have an error:
Error using ==> feval
Undefined function or method 'function' for input arguments of type
'double'.

It looks like fsolve doesn't see the file function.m which i write just before with fopen and fprintf. If i run again the script, it calculates and fsolve works fine.

But my problem is:
How ti eliminate the problem with writing a file and force fsolve to see the file function.m just after saving it,  before running again the script. This error interrupts the loops I am usuing. It is very important to me, becasuse I have to run again the script manually. I have to use fsolve in a loop for different equations and coefficients, and running it manually for every case is not possible.

It is common that I cannot use in currently running script a function that was created just before in this script?

Of course any other solutions to use fsolve without creating file function.m this way are also welcomed.

This problem doesn't look so complicated but I am a new Matlab user and I hope someone will help me :)

```
 0

See related articles to this posting

```
"Michal " <michalf111@gmail.com> wrote in message
news:i4u81l\$atd\$1@fred.mathworks.com...
> Hello everybody,
>
> I'm trying to solve a set of nonlinear equations using fsolve. But I have
> a problem. My script looks similar to this:
> .....
> .....First part is quite long and not very interesting. I calculate vector
> F, which is needed to use in fsolve. After calculations, I have, saved as
> a char:
> aaa = 'F= [x(1)*x(2)-2; x(1) - x(2)*x(1) - 2];'
> This is very simple case, normally I have 64 equations with x from x(1) to
> x(64). where aaa is a char.
> Then I use fprintf to write this to file:
>
> fid = fopen('function.m', 'w');

FUNCTION is a keyword in MATLAB (see ISKEYWORD); because of this you cannot
create and use a function file named function.m.

> fprintf(fid,'function F = funkcja(x)\n');
> fprintf(fid,'%s',aaa);
> fclose(fid);
>
> It works good, when I open this file (function.m) on hard disk it looks
> exactly like it supposed to look:
> function F = function(x)
> F= [x(1)*x(2)-2; x(1) - x(2)*x(1) - 2];
>
> Then I define x0 to use in fsolve:
> x0 = ones(2,1);
> And I try to use fsolve:
> [x1,x2] = fsolve(@function,x0);
>
> I have an error: Error using ==> feval
> Undefined function or method 'function' for input arguments of type
> 'double'.
>
> It looks like fsolve doesn't see the file function.m which i write just
> before with fopen and fprintf. If i run again the script, it calculates
> and fsolve works fine.

Why do you need to create a function dynamically like this?  It could cause
a race condition (where FSOLVE tries to run the function but it has not yet
been fully written.)

> But my problem is:
> How ti eliminate the problem with writing a file and force fsolve to see
> the file function.m just after saving it,  before running again the
> script. This error interrupts the loops I am usuing. It is very important
> to me, becasuse I have to run again the script manually. I have to use
> fsolve in a loop for different equations and coefficients, and running it
> manually for every case is not possible.

Can you describe your problem in a bit more detail?  Perhaps there's a
solution that doesn't require dynamically creating functions at runtime.

> It is common that I cannot use in currently running script a function that
> was created just before in this script?
>
> Of course any other solutions to use fsolve without creating file
> function.m this way are also welcomed.
>
> This problem doesn't look so complicated but I am a new Matlab user and I
> hope someone will help me :)
>

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
http://www.mathworks.com

```
 0

```"Michal " <michalf111@gmail.com> wrote in message <i4u81l\$atd\$1@fred.mathworks.com>...
> Hello everybody,
>
> I'm trying to solve a set of nonlinear equations using fsolve. But I have a problem. My script looks similar to this:
> .....
> .....First part is quite long and not very interesting. I calculate vector F, which is needed to use in fsolve. After calculations, I have, saved as a char:
> aaa = 'F= [x(1)*x(2)-2; x(1) - x(2)*x(1) - 2];'
>
> This is very simple case, normally I have 64 equations with x from x(1) to x(64).
> where aaa is a char.
> Then I use fprintf to write this to file:
>
> fid = fopen('function.m', 'w');
> fprintf(fid,'function F = funkcja(x)\n');
> fprintf(fid,'%s',aaa);
> fclose(fid);

It's a bad idea to write new Mfiles programmatically. You should explain why you think you have to do this, since there are safer/better ways to dynamically create a function. For example, why not just use anonymous functions, e.g.,

function= str2func( ['@(x)' F]  );

x=fsolve(@function,x0);

In any case, the reason for your problem is that MATLAB already has a built-in function named "function". Although you can over-ride builtin MATLAB functions with your own Mfiles, creating the file at run-time means MATLAB won't be aware of it, unless you issue a rehash() command.
```
 0

```"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i4ub1q\$qaj\$1@fred.mathworks.com>...

> It's a bad idea to write new Mfiles programmatically. You should explain why you think you have to do this, since there are safer/better ways to dynamically create a function. For example, why not just use anonymous functions, e.g.,
>
> function= str2func( ['@(x)' F]  );
>
> x=fsolve(@function,x0);
>
=======================

Sorry, Steve is right about "function" being a reserved keyword (I was thinking of something different). You should modify the above to the following

h= str2func( ['@(x)' F]  );
x=fsolve(h,x0);
```
 0

```"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i4ubg8\$pu2\$1@fred.mathworks.com>...
> "Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i4ub1q\$qaj\$1@fred.mathworks.com>...
>
> > It's a bad idea to write new Mfiles programmatically. You should explain why you think you have to do this, since there are safer/better ways to dynamically create a function. For example, why not just use anonymous functions, e.g.,
> >
> > function= str2func( ['@(x)' F]  );
> >
> > x=fsolve(@function,x0);
> >
> =======================
>
> Sorry, Steve is right about "function" being a reserved keyword (I was thinking of something different). You should modify the above to the following
>
>
> h= str2func( ['@(x)' F]  );
>  x=fsolve(h,x0);

Thank you very much! Anonymous function works exactly like I wanted.
```
 0

```"Michal " <michalf111@gmail.com> wrote in message <i4uetk\$bcq\$1@fred.mathworks.com>...

> > h= str2func( ['@(x)' F]  );
> >  x=fsolve(h,x0);
>
> Thank you very much! Anonymous function works exactly like I wanted.
===============

Since you're new to MATLAB, make sure you read about nested functions as well. They're another a common tool for dynamically defining functions to be processed by the Optimization Toolbox.
```
 0

```"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i4up1c\$h00\$1@fred.mathworks.com>...
> "Michal " <michalf111@gmail.com> wrote in message <i4uetk\$bcq\$1@fred.mathworks.com>...
>
> > > h= str2func( ['@(x)' F]  );
> > >  x=fsolve(h,x0);
> >
> > Thank you very much! Anonymous function works exactly like I wanted.
> ===============
>
> Since you're new to MATLAB, make sure you read about nested functions as well. They're another a common tool for dynamically defining functions to be processed by the Optimization Toolbox.

Hi all,
May I know what is wrong with my code? When i run the code, error message 'Function definitions are not permitted in this context.' keeps popping up. I copied this code from the example given in the function browser for fsolve.

function F = myfun(x)
F = [x(1)-(130^2)*0.41351*16*x(2)/(2*44145);
x(1)-x(2)/sqrt((0.015+0.15*(x(2))^2)^2+(x(2))^2)];
x0 = [0; 0];
options=optimset('Display','iter');
[x,fval] = fsolve(@myfun,x0,options);

Thanks! Your help is very much appreciated!
```
 0