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:
fsolve for range of parameters

Subject: fsolve for range of parameters

From: Maria

Date: 26 Nov, 2009 11:19:03

Message: 1 of 4

Dear all,

I am struggling a little with fsolve when it comes to applying using a range of parameters.
I written a function file that goes like this:

function F=myfunction(x, a,b,c,d) % where x=(x(1),x(2)) and a,b,c are parameters

F=[ x(1)^2 +a *x(2)+ b./(1-x(1));
x(2)^2 +c *x(1)+ b./(1-x(2))]
end

% above is just an example.

IN the main file I define the parameters and look for the zeros of the function:
 a=0.1
b=0.3
c=0.9
x0=[0.1, 0.2]

myf=@(x) myfunction(x,a,b,c)
x= fsolve(myf, x0)

AND THE PROGRAM WORKS!

BUT:

I want to find solutions for a range of the parameter c (for example):
c=0.1:0.01:0.99

and I would like this solutions to be stored in matrix, so that I can plot x(1) and x(2) against c , to see how the solution varies with the parameter.

I have tried many things, including:

for i=1:size(c,2)
myf(i) =@(x) myfunction(x,a,b,c(i))
x(i)=fsolve(myf(i),x0)

end
but it doesnt work.
HOW DO I STORE THE SOLUTIONS FOR ALL THE myf(i)'s THAT I AM INTERESTED IN?

thanks a lot

Mary

p.s I have a matlab 7.0.6...if this makes any difference....

Subject: fsolve for range of parameters

From: Torsten Hennig

Date: 26 Nov, 2009 11:38:28

Message: 2 of 4

> Dear all,
>
> I am struggling a little with fsolve when it comes to
> applying using a range of parameters.
> I written a function file that goes like this:
>
> function F=myfunction(x, a,b,c,d) % where
> x=(x(1),x(2)) and a,b,c are parameters
>
> F=[ x(1)^2 +a *x(2)+ b./(1-x(1));
> x(2)^2 +c *x(1)+ b./(1-x(2))]
> end
>
> % above is just an example.
>
> IN the main file I define the parameters and look for
> the zeros of the function:
> a=0.1
> b=0.3
> c=0.9
> x0=[0.1, 0.2]
>
> myf=@(x) myfunction(x,a,b,c)
> x= fsolve(myf, x0)
>
> AND THE PROGRAM WORKS!
>
> BUT:
>
> I want to find solutions for a range of the parameter
> c (for example):
> c=0.1:0.01:0.99
>
> and I would like this solutions to be stored in
> matrix, so that I can plot x(1) and x(2) against c ,
> to see how the solution varies with the parameter.
>
> I have tried many things, including:
>
> for i=1:size(c,2)
> myf(i) =@(x) myfunction(x,a,b,c(i))
> x(i)=fsolve(myf(i),x0)
>
> end

 for i=1:size(c,2)
 ci = c(i)
 x = fsolve(@(x) myfunction(x,a,b,ci),x0)
 x1(i) = x(1)
 x2(i) = x(2)
 end
 
 should work, I guess.

> but it doesnt work.
> HOW DO I STORE THE SOLUTIONS FOR ALL THE myf(i)'s
> THAT I AM INTERESTED IN?
>
> thanks a lot
>
> Mary
>
> p.s I have a matlab 7.0.6...if this makes any
> difference....

Subject: fsolve for range of parameters

From: Maria

Date: 26 Nov, 2009 15:48:06

Message: 3 of 4

thanks a lot!

It worked!!!

Torsten Hennig <Torsten.Hennig@umsicht.fhg.de> wrote in message <614411002.40335.1259235538924.JavaMail.root@gallium.mathforum.org>...
> > Dear all,
> >
> > I am struggling a little with fsolve when it comes to
> > applying using a range of parameters.
> > I written a function file that goes like this:
> >
> > function F=myfunction(x, a,b,c,d) % where
> > x=(x(1),x(2)) and a,b,c are parameters
> >
> > F=[ x(1)^2 +a *x(2)+ b./(1-x(1));
> > x(2)^2 +c *x(1)+ b./(1-x(2))]
> > end
> >
> > % above is just an example.
> >
> > IN the main file I define the parameters and look for
> > the zeros of the function:
> > a=0.1
> > b=0.3
> > c=0.9
> > x0=[0.1, 0.2]
> >
> > myf=@(x) myfunction(x,a,b,c)
> > x= fsolve(myf, x0)
> >
> > AND THE PROGRAM WORKS!
> >
> > BUT:
> >
> > I want to find solutions for a range of the parameter
> > c (for example):
> > c=0.1:0.01:0.99
> >
> > and I would like this solutions to be stored in
> > matrix, so that I can plot x(1) and x(2) against c ,
> > to see how the solution varies with the parameter.
> >
> > I have tried many things, including:
> >
> > for i=1:size(c,2)
> > myf(i) =@(x) myfunction(x,a,b,c(i))
> > x(i)=fsolve(myf(i),x0)
> >
> > end
>
> for i=1:size(c,2)
> ci = c(i)
> x = fsolve(@(x) myfunction(x,a,b,ci),x0)
> x1(i) = x(1)
> x2(i) = x(2)
> end
>
> should work, I guess.
>
> > but it doesnt work.
> > HOW DO I STORE THE SOLUTIONS FOR ALL THE myf(i)'s
> > THAT I AM INTERESTED IN?
> >
> > thanks a lot
> >
> > Mary
> >
> > p.s I have a matlab 7.0.6...if this makes any
> > difference....

Subject: fsolve for range of parameters

From: Eric

Date: 29 Jul, 2014 22:07:10

Message: 4 of 4

Sure, the loop works. But Matlab prefers simple matrix commands to replace loops whenever possible. How can this question be answered with matrix commands instead of the loop that Maria suggested?

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