Using fsolve with an interval of values

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.

Answers (1)

Torsten
Torsten on 15 Jul 2015
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 Comments

Nick Barker
Nick Barker on 15 Jul 2015
Edited: Nick Barker on 15 Jul 2015
Hello Torsten:
Thank you for your suggestions. 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 /= 0 and solve and plot for x(1) and x(2). I can provide the graph I am trying to replicate if that will help make my question clearer.
Again, thank you. I truly appreciate it.
Unfortunately, it did not help, but thanks anyway.
So you want to vary c and plot x(1) vs. x(2) for different values of c ?
Best wishes
Torsten.
That I believe is the case. You see, different values of c, which are chosen by the user, will affect the set-up of F(1,1)=0 and F(2, 1)=0. I need to find solutions x(1) and x(2) that satisfy both of the aforementioned equations.
However, it seems that I can take a slight shortcut because the graph I am trying to replicate already use for values of x(1) an interval of 0 to 6*pi with increments of pi. Which I believe means that I only need to solve for x(2) that satisfy both of the equations.
With that said, would it be more beneficial to have fsolve find values of both x(1) and x(2) and make a plot that way? If so, what syntax and topics should I look into?
I apologize for asking so many questions, but I am still pretty lost when it comes to a lot of Matlab's more advance features.
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(1,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,1) = 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.
Hello Torsten:
First and foremost, thank you for all of your help and input. It is truly appreciated, and I am sure you are busy with your own responsibilities.
Unfortunately, the code seems to run into an error at line 14, F(1,1).
The error is as follows: Subscripted assignment dimension mismatch.
I assume that means my arrays do not have equivalent dimensions. Any suggestions would be appreciated.
Thank you.
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.
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.

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Products

Asked:

on 15 Jul 2015

Edited:

on 17 Jul 2015

Community Treasure Hunt

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

Start Hunting!