Using fsolve with an interval of values

3 次查看(过去 30 天)
Hello:
I am having some issues with fsolve in relation to using it for equations on an interval of 0 to 6pi with an increment of pi. I have made a function file that writes the set of equations as such:
function F=eqns(x, c)
% p=x(1) and q=x(2); see Padouissis article for more information; F{1}=0 and F{2}=0
x(1) = 0:pi:6*pi;
F{1} = (x(1).^(2).*x(2).^(2).*(x(2).^(2)+x(1).^(2))+c^(2).*(x(1).^(4)+x(2).^4-6.*x(1).^(2).*x.^(2))-3*c^(4).*(x(1).^(2)-x(2).^(2))-4.*c^(6)).*sin(x(1)).*sinh(x(2))+2.*x(1).*x(2).*(-x(1).^(2).*x(2).^(2)+3.*c^(2).*(x(1).^(2)-x(2).^(2))-7.*c^(4)).*cos(x(1)).*cosh(x(2))-x(1).*x(2).*(x(1).^(4)+x(2).^(4)+2.*c^(2).*(x(2).^(2)-x(1).^(2))+2.*c^(4)).*cos(2*c);
F{2} = 2.*c.*x(1).*(3.*x(1).^(2).*x(2).^(2)-x(2).^(4)-c^(2).*(x(1).^(2)+x(2).^(2))+4.*c^(4)).*cos(x(1)).*sinh(x(2))+2.*c.*x(2).*(x(1).^(4)-3.*x(2).^(2).*x(1).^(2)-c^(2).*(x(1).^(2)+x(2).^(2))-4*c^(2)).*sin(x(1)).*cosh(x(2))+x(1).*x(2).*((x(1).^(2)+x(2).^(2)).*(x(2).^(2))-x(1).^(2)+2.*c^(2)).*sin(2*c); % code
Then use the user made function (a code that was devised by a user on the forums, you have been a great help, and it is greatly appreciated), eqns_driver:
function eqns_driver
c = input('input value c');
x0 = [2 2]; %replace with initial conditions appropriate for your purpose
result = fsolve(@(x) eqns(x, c), x0);
My ultimate goal is to make a plot with x(1) and x(2).
Thank you. I greatly appreciate it.
Edit: I apologize if my question was unclear. I am currently trying to replicate a special case of the equation systems where c = 0, and thus the values of x(1) are already given. However, I would like to make a program where c can be any real number that the user wishes to input and solve and plot for those x(1) and x(2).
The value of c is not something I am trying to solve for or plot, but only determines how the two simultaneous equations will be set up. I can provide the graph I am trying to replicate if that will help make my question clearer.

回答(1 个)

Torsten
Torsten 2015-7-15
Use F(1,1)=... and F(2,1)=... instead of F{1}=... and F{2}=...
Furthermore, it's not clear to me which variable you want to vary.
If it's really x(1) as written, you only need one equation to calculate x(2), not two.
Best wishes
Torsten.
  7 个评论
Torsten
Torsten 2015-7-17
Try
function main
C=0:0.1:5;
x0=[0 ; 0];
for i=1:length(C)
c=C(i);
result = fsolve(@(x) eqns(x, c), x0);
RESULT(1,i)=result(1);
RESULT(2,i)=result(2);
x0=[result(1) ; result(2)];
end
plot(RESULT(1,:),RESULT(2,:));
function F=eqns(x, c)
F = [(x(1).^(2).*x(2).^(2).*(x(2).^(2)+x(1).^(2))+c^(2).*(x(1).^(4)+x(2).^4-6.*x(1).^(2).*x.^(2))-3*c^(4).*(x(1).^(2)-x(2).^(2))-4.*c^(6)).*sin(x(1)).*sinh(x(2))+2.*x(1).*x(2).*(-x(1).^(2).*x(2).^(2)+3.*c^(2).*(x(1).^(2)-x(2).^(2))-7.*c^(4)).*cos(x(1)).*cosh(x(2))-x(1).*x(2).*(x(1).^(4)+x(2).^(4)+2.*c^(2).*(x(2).^(2)-x(1).^(2))+2.*c^(4)).*cos(2*c);
2.*c.*x(1).*(3.*x(1).^(2).*x(2).^(2)-x(2).^(4)-c^(2).*(x(1).^(2)+x(2).^(2))+4.*c^(4)).*cos(x(1)).*sinh(x(2))+2.*c.*x(2).*(x(1).^(4)-3.*x(2).^(2).*x(1).^(2)-c^(2).*(x(1).^(2)+x(2).^(2))-4*c^(2)).*sin(x(1)).*cosh(x(2))+x(1).*x(2).*((x(1).^(2)+x(2).^(2)).*(x(2).^(2))-x(1).^(2)+2.*c^(2)).*sin(2*c)];
Best wishes
Torsten.
Nick Barker
Nick Barker 2015-7-17
Hello Torsten:
While fsolve did process it. It did not give me the graph I am trying to replicate. In fact, I got an empty graph.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by