Thread Subject: symbolic expression to function

Subject: symbolic expression to function

From: mohammad movassat

Date: 29 Jun, 2009 23:34:01

Message: 1 of 9

Hi,

I have 10 symbolic expressions with 10 variables. I want to solve them by fsolve, so I need to change from symbolic to function first. The Matlab version I have, does not support fo matlabfunction in symbolic math toolbox. Any suggestions?

Thank you all,

Mohammad

Subject: symbolic expression to function

From: Matt Johnson

Date: 30 Jun, 2009 05:14:02

Message: 2 of 9

I assume you mean that you have 10 linear equations with 10 variables. You can solve this using Cramer's Rule and use matlab to compute the determinates.

http://en.wikipedia.org/wiki/Cramer%27s_rule

Subject: symbolic expression to function

From: Matt Johnson

Date: 30 Jun, 2009 05:38:01

Message: 3 of 9

"Matt Johnson" <mattjohnson00@gmail.com> wrote in message <h2c6uq$665$1@fred.mathworks.com>...
> I assume you mean that you have 10 linear equations with 10 variables. You can solve this using Cramer's Rule and use matlab to compute the determinates.
>
> http://en.wikipedia.org/wiki/Cramer%27s_rule

Actually I though of a better way: help linsolve

Subject: symbolic expression to function

From: Loren Shure

Date: 30 Jun, 2009 11:53:58

Message: 4 of 9

In article <h2bj19$ap1$1@fred.mathworks.com>, movassat@gmail.com says...
> Hi,
>
> I have 10 symbolic expressions with 10 variables. I want to solve them by fsolve, so I need to change from symbolic to function first. The Matlab version I have, does not support fo matlabfunction in symbolic math toolbox. Any suggestions?
>
> Thank you all,
>
> Mohammad
>

I don't know of way for you to do it automatically without upgrading.
You could hand-translate your expressions and place them in a file for
use with fsolve. The functions char and vectorize may help you.


--
Loren
http://blogs.mathworks.com/loren

Subject: symbolic expression to function

From: Alan Weiss

Date: 30 Jun, 2009 12:51:26

Message: 5 of 9

Loren Shure wrote:
> In article <h2bj19$ap1$1@fred.mathworks.com>, movassat@gmail.com says...
>> Hi,
>>
>> I have 10 symbolic expressions with 10 variables. I want to solve them by fsolve, so I need to change from symbolic to function first. The Matlab version I have, does not support fo matlabfunction in symbolic math toolbox. Any suggestions?
>>
>> Thank you all,
>>
>> Mohammad
>>
>
> I don't know of way for you to do it automatically without upgrading.
> You could hand-translate your expressions and place them in a file for
> use with fsolve. The functions char and vectorize may help you.
>
>
doc subs
or
doc sym/subs

You can use the subs command to evaluate a symbolic expression
numerically. Write an M-file along the following lines:
function val = myfun(x,symvars)
val = subs(symExpression,symvars,{x});

symExpression is the symbolic expression that you want to equal a vector
of zeros.

Before running fsolve, set symvars = {var1,...,var10} in your workspace.
Call fsolve with the syntax
answer = fsolve(@(x)myfun(x,symvars),x0)

Alan Weiss
MATLAB mathematical toolbox documentation

Subject: symbolic expression to function

From: mohammad movassat

Date: 30 Jun, 2009 16:25:04

Message: 6 of 9

Alan Weiss <aweiss@mathworks.com> wrote in message <h2d1of$jg9$1@fred.mathworks.com>...
> Loren Shure wrote:
> > In article <h2bj19$ap1$1@fred.mathworks.com>, movassat@gmail.com says...
> >> Hi,
> >>
> >> I have 10 symbolic expressions with 10 variables. I want to solve them by fsolve, so I need to change from symbolic to function first. The Matlab version I have, does not support fo matlabfunction in symbolic math toolbox. Any suggestions?
> >>
> >> Thank you all,
> >>
> >> Mohammad
> >>
> >
> > I don't know of way for you to do it automatically without upgrading.
> > You could hand-translate your expressions and place them in a file for
> > use with fsolve. The functions char and vectorize may help you.
> >
> >
> doc subs
> or
> doc sym/subs
>
> You can use the subs command to evaluate a symbolic expression
> numerically. Write an M-file along the following lines:
> function val = myfun(x,symvars)
> val = subs(symExpression,symvars,{x});
>
> symExpression is the symbolic expression that you want to equal a vector
> of zeros.
>
> Before running fsolve, set symvars = {var1,...,var10} in your workspace.
> Call fsolve with the syntax
> answer = fsolve(@(x)myfun(x,symvars),x0)
>
> Alan Weiss
> MATLAB mathematical toolbox documentation

Hi

thanks for all of your responses. My equations are VERY nonlinear, so I can not use linear solvers. The problem is that I'm new in MATLAB and I do not understand some of your comments. Here is how I generate my 10 equations:
> x = sym('x');
> a = sym(zeros(1, 10));
> for k = 1:10, a(k) = sym(sprintf('a%d', k)); end
> f = poly2sym(a); % a very long equations in terms of x and a(1)-a(10)
> df = diff(f, x);

g = cell(1, 10);
> for k = 1:10
> g{k} = subs(df, x, specifiedX(k));
> end

At this point I have 10 nonlinear equations in terms of a(1) -a(10) and I want to solve them by fsolve. From the last reply I understand that I have to define functions manually. But as I said, since I'm new in MATLAB, I could not understand how. Could you please provide me with more details.

thanks alot,

Mohammad

Subject: symbolic expression to function

From: Steven Lord

Date: 30 Jun, 2009 17:09:41

Message: 7 of 9


"mohammad movassat" <movassat@gmail.com> wrote in message
news:h2de90$egb$1@fred.mathworks.com...

*snip*

> At this point I have 10 nonlinear equations in terms of a(1) -a(10) and I
> want to solve them by fsolve. From the last reply I understand that I have
> to define functions manually. But as I said, since I'm new in MATLAB, I
> could not understand how. Could you please provide me with more details.

Use CHAR to convert the symbolic expression into a character array and then
FPRINTF it into a function M-file. Note that if you use this approach, you
may need to adjust the expression if it calls any functions that are part of
Maple but not part of MATLAB.

--
Steve Lord
slord@mathworks.com

Subject: symbolic expression to function

From: mohammad movassat

Date: 30 Jun, 2009 23:03:01

Message: 8 of 9

"Steven Lord" <slord@mathworks.com> wrote in message <h2dgr8$7tc$1@fred.mathworks.com>...
>
> "mohammad movassat" <movassat@gmail.com> wrote in message
> news:h2de90$egb$1@fred.mathworks.com...
>
> *snip*
>
> > At this point I have 10 nonlinear equations in terms of a(1) -a(10) and I
> > want to solve them by fsolve. From the last reply I understand that I have
> > to define functions manually. But as I said, since I'm new in MATLAB, I
> > could not understand how. Could you please provide me with more details.
>
> Use CHAR to convert the symbolic expression into a character array and then
> FPRINTF it into a function M-file. Note that if you use this approach, you
> may need to adjust the expression if it calls any functions that are part of
> Maple but not part of MATLAB.
>
> --
> Steve Lord
> slord@mathworks.com
>

Hi,

It might look a stupid question, but what I can see in matlab help for fprintf is for writing data to a file:

http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/fprintf.html&http://www.google.com/search?q=fprintf+matlab&rls=com.microsoft:*:IE-SearchBox&ie=UTF-8&oe=UTF-8&sourceid=ie7

I could not find the way I can write a "char" to a function m-file. could you please do me a favor? assume you have f=char(g) and g is your expression in terms of x and y. what commands you will write to write f in a function m-file? thank you for your helps

Subject: symbolic expression to function

From: TideMan

Date: 30 Jun, 2009 23:55:53

Message: 9 of 9

On Jul 1, 11:03 am, "mohammad movassat" <movas...@gmail.com> wrote:
> "Steven Lord" <sl...@mathworks.com> wrote in message <h2dgr8$7t...@fred.mathworks.com>...
>
> > "mohammad movassat" <movas...@gmail.com> wrote in message
> >news:h2de90$egb$1@fred.mathworks.com...
>
> > *snip*
>
> > > At this point I have 10 nonlinear equations in terms of a(1) -a(10) and I
> > > want to solve them by fsolve. From the last reply I understand that I have
> > > to define functions manually. But as I said, since I'm new in MATLAB, I
> > > could not understand how. Could you please provide me with more details.
>
> > Use CHAR to convert the symbolic expression into a character array and then
> > FPRINTF it into a function M-file.  Note that if you use this approach, you
> > may need to adjust the expression if it calls any functions that are part of
> > Maple but not part of MATLAB.
>
> > --
> > Steve Lord
> > sl...@mathworks.com
>
> Hi,
>
> It might look a stupid question, but what I can see in matlab help for fprintf is for writing data to a file:
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/acc...
>
> I could not find the way I can write a "char" to a function m-file. could you please do me a favor? assume you have f=char(g) and g is your expression in terms of x and y. what commands you will write to write f in a function m-file? thank you for your helps

fid=fopen('stupid.m','wt');
fprintf(fid,'%s',f);
fclose(fid);

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com