Solving a system of equations in matlab

1 view (last 30 days)
I'm trying to solve a system of equations in matlab that is parametrized in A. The equations are: x^2+Ay^2=12 x*y=3 and A is parametrized from 1/3 to 3.
my solution thus far:
I created a function file to solve for x and y x=x(1) and y=x(2)
function F = functionfile(x,A) F(1)=x(1).*x(1)- A.*(x(2).*x(2)) -12; F(2)=x(1).*x(2)-3; end
and I solve it in my main file:
x0=[1;1]; A=2; options=optimset('Display','off'); x=fsolve(@functionfile,x0,options,A);
My problem is, when I try to make A a vector of 1/3:1/3:3, it returns an error message that doesn't occur when I give A a single value. How do I fix this problem?

Accepted Answer

Sven
Sven on 16 Mar 2013
Edited: Sven on 16 Mar 2013
Hi E,
There's a main issue that causes your problem here. Firstly, imagine that you had got inside your function functionfile with x=[1;1] and A=1/3:1/3:3. You would hit the very first line which is:
F(1)=x(1).*x(1)- A.*(x(2).*x(2)) -12;
Do you see how this cannot work? The output on the right-hand side is a 1-by-9 array, but you're trying to put it all into the single element F(1).
A couple of hints:
  1. You can put a breakpoint on the first line of your functionfile and check this out yourself (because MATLAB errors inside optimisations like fsolve are a little difficult to track down)
  2. You can format your code in the question above to make it a bit easier to read (your question is actually a good one... it's just a little squashed)
  3. Can you explain what kind of answer you expect when A is a vector? Perhaps you'd just like to loop over each element of A and solve it independently?
A = 1/3:1/3:3
x = zeros(2,length(A))
for i = 1:length(A)
x(:,i) = fsolve(@tmpfunc,x0,options,A(i));
end
Hope this answered your question.

More Answers (1)

E
E on 16 Mar 2013
Thank you! I should have thought of the loop idea! That was exactly what I needed to do!

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!