Solving a system of equations in matlab

조회 수: 1 (최근 30일)
E
E 2013년 3월 15일
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?

채택된 답변

Sven
Sven 2013년 3월 16일
편집: Sven 2013년 3월 16일
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.

추가 답변 (1개)

E
E 2013년 3월 16일
Thank you! I should have thought of the loop idea! That was exactly what I needed to do!

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by