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:
Solve non-linear functions rapidly?

Subject: Solve non-linear functions rapidly?

From: Jani Korhonen

Date: 3 Apr, 2011 17:52:05

Message: 1 of 5

Dear all,

I want to solve this function C1 * x^2 - exp(C2 * x) = 0 in Matlab, in which C1 and C2 are constants. I know it is easy to use fsolve to solve it, e.g. x=fsolve((x)C1*x^2-exp(C2*x), 0). My question is that x is a matrix, and I want to solve this equation at each point, i.e. obtaining an individual x value at each point. C1 and C2 might be different at different points, but they can be obtained in advance.

However, if I use a loop to find out the answer at each point, it takes a lot of time. But if I use the following: X = fsolve((X), C1*X^2-exp(C2*X), zeros(size(X, 1), size(X, 2)), where X is a matrix. Matlab says that x must be square. It seems the function was not treated at individual points. So, how can I solve this equation rapidly? Thank you very much.

Best regards,

Jani

Subject: Solve non-linear functions rapidly?

From: Nasser M. Abbasi

Date: 3 Apr, 2011 18:13:26

Message: 2 of 5

On 4/3/2011 10:52 AM, Jani Korhonen wrote:
> Dear all,
>
> I want to solve this function C1 * x^2 - exp(C2 * x) = 0 in Matlab, in which C1 and C2 are constants.
> I know it is easy to use fsolve to solve it, e.g. x=fsolve((x)C1*x^2-exp(C2*x), 0). My question is that x is
> a matrix, and I want to solve this equation at each point, i.e. obtaining an individual x value at each point.
> C1 and C2 might be different at different points, but they can be obtained in advance.
>
> However, if I use a loop to find out the answer at each point, it takes a lot of time. But if
> I use the following: X = fsolve((X), C1*X^2-exp(C2*X), zeros(size(X, 1), size(X, 2)), where X is a
> matrix. Matlab says that x must be square. It seems the function was not treated at individual points.
> So, how can I solve this equation rapidly? Thank you very much.
>
> Best regards,
>
> Jani

If I understand you right, this is what you want to do:

Find the roots of f(x) near number of points in one call.

Right?

Then why not just list those points in an array, and pass
that array to fsolve? Why do you need this matrix thing for?

EDU>> points_to_find_roots_near = [1 2 3 4 5 6];
EDU>> fsolve(@(x) sin(3*x),points_to_find_roots_near )

ans =
     1.0472 2.0944 3.1416 4.1888 5.2360 6.2832

You might have to change the above to handle different C1, C2 in your
equation, but I am here just talking about the need to supply
the points.

--Nasser

Subject: Solve non-linear functions rapidly?

From: Roger Stafford

Date: 3 Apr, 2011 18:19:05

Message: 3 of 5

"Jani Korhonen" <eagle_nokia@hotmail.com> wrote in message <inac45$6lr$1@fred.mathworks.com>...
> Dear all,
>
> I want to solve this function C1 * x^2 - exp(C2 * x) = 0 in Matlab, in which C1 and C2 are constants. I know it is easy to use fsolve to solve it, e.g. x=fsolve((x)C1*x^2-exp(C2*x), 0). My question is that x is a matrix, and I want to solve this equation at each point, i.e. obtaining an individual x value at each point. C1 and C2 might be different at different points, but they can be obtained in advance.
>
> However, if I use a loop to find out the answer at each point, it takes a lot of time. But if I use the following: X = fsolve((X), C1*X^2-exp(C2*X), zeros(size(X, 1), size(X, 2)), where X is a matrix. Matlab says that x must be square. It seems the function was not treated at individual points. So, how can I solve this equation rapidly? Thank you very much.
>
> Best regards,
>
> Jani
- - - - - - - - - -
  Substituting w = -C2/2*x puts your equation in the form of Lambert's W equation. Solutions for that can be obtained from the 'lambertw' function in the Symbolic Toolbox which accepts numeric matrices. That might be faster than using 'fsolve'.

Roger Stafford

Subject: Solve non-linear functions rapidly?

From: Jani Korhonen

Date: 3 Apr, 2011 18:21:04

Message: 4 of 5

Dear Nasser,

Thanks a lot for your help. But I don't think this is what I hope. Actually my question is very simple. I have a matrix X, and I want to find the root of each element in the matrix, i.e. C1*x(i,j)^2-exp(C2*x(i,j))=0. But I don't want to use loop.

BTW, if I want to find out the optimal POSITIVE root for each element, how can I do? Thank you very much.

Best regards,

Jani

"Nasser M. Abbasi" <nma@12000.org> wrote in message <inadc3$r5u$1@speranza.aioe.org>...
> On 4/3/2011 10:52 AM, Jani Korhonen wrote:
> > Dear all,
> >
> > I want to solve this function C1 * x^2 - exp(C2 * x) = 0 in Matlab, in which C1 and C2 are constants.
> > I know it is easy to use fsolve to solve it, e.g. x=fsolve((x)C1*x^2-exp(C2*x), 0). My question is that x is
> > a matrix, and I want to solve this equation at each point, i.e. obtaining an individual x value at each point.
> > C1 and C2 might be different at different points, but they can be obtained in advance.
> >
> > However, if I use a loop to find out the answer at each point, it takes a lot of time. But if
> > I use the following: X = fsolve((X), C1*X^2-exp(C2*X), zeros(size(X, 1), size(X, 2)), where X is a
> > matrix. Matlab says that x must be square. It seems the function was not treated at individual points.
> > So, how can I solve this equation rapidly? Thank you very much.
> >
> > Best regards,
> >
> > Jani
>
> If I understand you right, this is what you want to do:
>
> Find the roots of f(x) near number of points in one call.
>
> Right?
>
> Then why not just list those points in an array, and pass
> that array to fsolve? Why do you need this matrix thing for?
>
> EDU>> points_to_find_roots_near = [1 2 3 4 5 6];
> EDU>> fsolve(@(x) sin(3*x),points_to_find_roots_near )
>
> ans =
> 1.0472 2.0944 3.1416 4.1888 5.2360 6.2832
>
> You might have to change the above to handle different C1, C2 in your
> equation, but I am here just talking about the need to supply
> the points.
>
> --Nasser
>

Subject: Solve non-linear functions rapidly?

From: Jani Korhonen

Date: 3 Apr, 2011 18:33:04

Message: 5 of 5

Great, Roger, thanks a lot. Your suggestion is wonderful.

"Roger Stafford" wrote in message <inadmp$8u$1@fred.mathworks.com>...
> "Jani Korhonen" <eagle_nokia@hotmail.com> wrote in message <inac45$6lr$1@fred.mathworks.com>...
> > Dear all,
> >
> > I want to solve this function C1 * x^2 - exp(C2 * x) = 0 in Matlab, in which C1 and C2 are constants. I know it is easy to use fsolve to solve it, e.g. x=fsolve((x)C1*x^2-exp(C2*x), 0). My question is that x is a matrix, and I want to solve this equation at each point, i.e. obtaining an individual x value at each point. C1 and C2 might be different at different points, but they can be obtained in advance.
> >
> > However, if I use a loop to find out the answer at each point, it takes a lot of time. But if I use the following: X = fsolve((X), C1*X^2-exp(C2*X), zeros(size(X, 1), size(X, 2)), where X is a matrix. Matlab says that x must be square. It seems the function was not treated at individual points. So, how can I solve this equation rapidly? Thank you very much.
> >
> > Best regards,
> >
> > Jani
> - - - - - - - - - -
> Substituting w = -C2/2*x puts your equation in the form of Lambert's W equation. Solutions for that can be obtained from the 'lambertw' function in the Symbolic Toolbox which accepts numeric matrices. That might be faster than using 'fsolve'.
>
> Roger Stafford

Tags for this Thread

No tags are associated with 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