- 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)
- 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)
- 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?
Solving a system of equations in matlab
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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?
0 commentaires
Réponse acceptée
Sven
le 16 Mar 2013
Modifié(e) : Sven
le 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:
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.
0 commentaires
Plus de réponses (1)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!