Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

fsolve for a range of parameters

190 views
Skip to first unread message

Jasmin

unread,
Apr 3, 2012, 11:30:18 PM4/3/12
to
Hi,

I have two non-linear equations to solve two unknowns x and y:
f1(x,y,phi)=0
f2(x,y,phi)=0
where phi is a parameter and 0<phi<1.

I want to look at how x changes given different values of phi within its range. I tried using fsolve by typing the following commands:

function F = myfunction(unknown)

F=[f1(x,y,phi);
f2(x,y,phi);]

phi=0.01:0.01:0.99;

unknown0=[1;1]

for i=1:size(phi,2)
unknown = fsolve(@myfunction, unknown0)
x(i) = unknown(1)
y(i) = unknown(2)
end

It returned on Matlab the following error message:
Matrix dimensions must agree.

Is there anything wrong with the commands? Or perhaps it's not appropriate to use fsolve here?

Any help would be much appreciated.

Thanks!

Marc

unread,
Apr 3, 2012, 11:56:14 PM4/3/12
to
"Jasmin" wrote in message <jlgf8a$r5a$1...@newscl01ah.mathworks.com>...
The problem is with your statement "size(phi)" which will return two numbers the length and 1.

I always use a statement like m = max(size(phi)); which will give you the the length but I think in your case length(phi) may also work....

Marc

unread,
Apr 4, 2012, 1:47:12 AM4/4/12
to
"Marc" wrote in message <jlggou$2pq$1...@newscl01ah.mathworks.com>...
I just noticed that your statement was size(phi,2), so that may not be your problem. Sorry.

I will have to check on my laptop when I have a second. Unless size( phi,2) returns 1 which I doubt.

Looking at this a second time, your problem may be that your functions haze two unknowns and require three variables. So fsolve is being given a function that requires 3 variables and you have only supplied two in your initial guess. Hence you get the return that your dimensions are off. If phi is not changing during your for loop, which appears to be the case from your code, albeit probably not your intention, you will have to rethink your approach.

Depending on how complex f1 and f2 are you may be able to use an anonymous function within a loop with your new 'phi' defined in front of the function still within the loop.

Something like this may work but I never know until I try...

for phi = 0.01:0.01:0.99

myfunc = @ myfunc(x) {f1(x(1),x(2),phi) ; f2(x(1),x(2), phi)};

fsolve (......)
end


Be careful assuming my syntax is correct as I am doing this from my tablet without Matlab in front of me.

Sargondjani

unread,
Apr 4, 2012, 5:12:17 AM4/4/12
to
I suggest you read this first on passing parameters:

http://www.mathworks.nl/help/toolbox/optim/ug/brhkghv-7.html

The thing above would probably work, but I would do it slightly different:

If this is your function, with x containing x(1) and x(2) (your x and y):

function F=myfunction(x,phi);
F=[f1(x(1),x(2),phi);
f2(x(1),x(2),phi);]
end

the you can use a loop:

for iphi=1:length(phi);
my_fun=@(x)myfunction(x,phi(iphi);
x_opt = fsolve(my_fun, x0)
x1(1,iphi)=x_opt(1);
x2(1,iphi)=x_opt(2);
end

make sure x0 has two entries. this should give you the x1's and x2's in row vector corresponding to the values of phi.
there is probably a more convenient way to do this, but it should work...

Jasmin

unread,
Apr 5, 2012, 3:39:35 PM4/5/12
to
"Sargondjani" wrote in message <jlh39h$4df$1...@newscl01ah.mathworks.com>...
Yes, it works. Thanks so much!

I realise if I define phi = 0.01:0.01:0.99 within the same .m-file as the function then it won't work. Is that because given only function F=myfunction(x,phi), Matlab doesn't recognise the dimensions of x(1) and x(2)?

Sargondjani

unread,
Apr 6, 2012, 2:58:18 AM4/6/12
to
> Yes, it works. Thanks so much!

you're welcome!


> I realise if I define phi = 0.01:0.01:0.99 within the same .m-file as the function then it won't work. Is that because given only function F=myfunction(x,phi), Matlab doesn't recognise the dimensions of x(1) and x(2)?

matlab will recognize x(1) and x(2) if you have defined them in the function. if your function is F=myfunction(x,phi) but defining phi inside the function, that doesnt make sense. then at least the function should have only one argument (x).

but if you would do it like that (so F=myfunction2(x) and defining phi as a row vector inside the function), then the two functions (f1 & f2) should give row vectors as answers, making F a matrix. This is not allowed as input into fsolve. you should check if indeed F is a matrix when you plug some x into myfunction2(x)
0 new messages