Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
MATLAB PROBLEM

Subject: MATLAB PROBLEM

From: Mamon

Date: 5 Sep, 2010 12:33:05

Message: 1 of 10

Hi ALL ;
I'm a PhD student at USC, I'm trying to use matlab to solve a group of equations ( I want to find the values of x(1),x(2),x(3),x(4),x(5),and x(6)), I'm trying to use fsolve in *.m file because I need the results to be written directly for output file, could you please help me in that, the code that I use is between the two lines (but it always gives me an error):
-----------------------------------------------------------------------------------------------------------------------
function F = myfun(x)
x=zeros(3,2);
F(1)=inline('(x(1)^2)+74.201-(-17.228*x(1))+(x(2)^2)+5.341-(4.622*x(2))+(x(3)^2)+165.431-(25.724*x(3))-1398.760','x');
F(2)=inline('(x(4)^2)+74.201-(-17.228*x(4))+(x(5)^2)+5.341-(4.622*x(5))+(x(6)^2)+165.431-(25.724*x(6))-750.760','x');
F(3)=inline('(x(1)^2)+32.217-(-11.352*x(1))+(x(2)^2)+49.323-(-14.046*x(2))+(x(3)^2)+341.289-(36.948*x(3))-858.490','x');
F(4)=inline('(x(4)^2)+32.217-(-11.352*x(4))+(x(5)^2)+49.323-(-14.046*x(5))+(x(6)^2)+341.289-(36.948*x(6))-327.610','x');
F(5)=inline('(x(1)^2)+13.542-(7.360*x(1))+(x(2)^2)+68.013-(16.494*x(2))+(x(3)^2)+45.239-(13.452*x(3))-1608.010','x');
F(6)=inline('(x(4)^2)+13.542-(7.360*x(4))+(x(5)^2)+68.013-(16.494*x(5))+(x(6)^2)+45.239-(13.452*x(6))-912.040','x');
F(7)=inline('x(1)^2-(2*x(1)*x(4))+x(4)^2+x(2)^2-(2*x(2)*x(5))+x(5)^2+x(3)^2-(2*x(3)*x(6))+x(6)^2-224.101','x');
x0 = [1; 1; 1; 1; 1; 1];
options=optimset('Display','iter');
x=fsolve(F,x0,options);
end
------------------------------------------------------------------------------------------------------------------------

also I have a question about how can we get all the solutions for the above equations, because fsolve now gives just one solution, and I wonder if there is a method to produce all the solutions or if we can change the initial guess by using " for loop".

any help is appreciated,
Thank you,
Ma'mon hatmal

Subject: MATLAB PROBLEM

From: Walter Roberson

Date: 5 Sep, 2010 17:05:40

Message: 2 of 10

On 05/09/10 7:33 AM, Mamon wrote:
> Hi ALL ; I'm a PhD student at USC, I'm trying to use matlab to solve a
> group of equations ( I want to find the values of
> x(1),x(2),x(3),x(4),x(5),and x(6)),

> also I have a question about how can we get all the solutions for the
> above equations, because fsolve now gives just one solution, and I
> wonder if there is a method to produce all the solutions or if we can
> change the initial guess by using " for loop".

I looked around a bit a couple of months ago to see if I could find any
known upper bound on the number of zeros of a multinomial, but I was
unsuccessful even in just two dimensions. I saw one argument sketched
out that the number of solutions must be infinite (at least if you
include imaginary solutions) even just for 2 variables.

Subject: MATLAB PROBLEM

From: Roger Stafford

Date: 5 Sep, 2010 17:17:08

Message: 3 of 10

"Mamon " <mamoon_hatmal@yahoo.com> wrote in message <i602m1$3v$1@fred.mathworks.com>...
> Hi ALL ;
> I'm a PhD student at USC, I'm trying to use matlab to solve a group of equations ( I want to find the values of x(1),x(2),x(3),x(4),x(5),and x(6)), I'm trying to use fsolve in *.m file because I need the results to be written directly for output file, could you please help me in that, the code that I use is between the two lines (but it always gives me an error):
> -----------------------------------------------------------------------------------------------------------------------
> function F = myfun(x)
> x=zeros(3,2);
> F(1)=inline('(x(1)^2)+74.201-(-17.228*x(1))+(x(2)^2)+5.341-(4.622*x(2))+(x(3)^2)+165.431-(25.724*x(3))-1398.760','x');
> F(2)=inline('(x(4)^2)+74.201-(-17.228*x(4))+(x(5)^2)+5.341-(4.622*x(5))+(x(6)^2)+165.431-(25.724*x(6))-750.760','x');
> F(3)=inline('(x(1)^2)+32.217-(-11.352*x(1))+(x(2)^2)+49.323-(-14.046*x(2))+(x(3)^2)+341.289-(36.948*x(3))-858.490','x');
> F(4)=inline('(x(4)^2)+32.217-(-11.352*x(4))+(x(5)^2)+49.323-(-14.046*x(5))+(x(6)^2)+341.289-(36.948*x(6))-327.610','x');
> F(5)=inline('(x(1)^2)+13.542-(7.360*x(1))+(x(2)^2)+68.013-(16.494*x(2))+(x(3)^2)+45.239-(13.452*x(3))-1608.010','x');
> F(6)=inline('(x(4)^2)+13.542-(7.360*x(4))+(x(5)^2)+68.013-(16.494*x(5))+(x(6)^2)+45.239-(13.452*x(6))-912.040','x');
> F(7)=inline('x(1)^2-(2*x(1)*x(4))+x(4)^2+x(2)^2-(2*x(2)*x(5))+x(5)^2+x(3)^2-(2*x(3)*x(6))+x(6)^2-224.101','x');
> x0 = [1; 1; 1; 1; 1; 1];
> options=optimset('Display','iter');
> x=fsolve(F,x0,options);
> end
> ------------------------------------------------------------------------------------------------------------------------
>
> also I have a question about how can we get all the solutions for the above equations, because fsolve now gives just one solution, and I wonder if there is a method to produce all the solutions or if we can change the initial guess by using " for loop".
>
> any help is appreciated,
> Thank you,
> Ma'mon hatmal
- - - - - - - - - -
  You are trying to solve seven different equations with only six unknowns. In general there would be no solution. Equations F(1), F(3), and F(5) represent the intersection between three spheres in x(1), x(2), and x(3) space and would have either two solutions, one solution, or none. Equations F(2), F(4), and F(6) give three spheres in x(4), x(5), and x(6) space with the same centers as in the other three spheres but with apparently different radii. The last equation F(7) is the assertion that the two solutions in the first set are a given distance from those in the second set. As you can see, this last would not be true in general. You should not be trying to solve all seven but only the three F(1), F(3), and F(5) together and then F(2), F(4), and F(6) together as a separate problem. Then you can use F(7) to check whether any combination from the other solutions satisfy it.

  If you are having trouble finding all solutions, you should know that there is a fairly direct way of solving the problem of the intersection of three spheres. It involves first finding where the line connecting the two solutions passes through the plane of the three centers, and next moving out along this line an appropriate distance.

Roger Stafford

Subject: MATLAB PROBLEM

From: Mamon

Date: 5 Sep, 2010 21:05:04

Message: 4 of 10

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i60jak$6ib$1@fred.mathworks.com>...
> "Mamon " <mamoon_hatmal@yahoo.com> wrote in message <i602m1$3v$1@fred.mathworks.com>...
> > Hi ALL ;
> > I'm a PhD student at USC, I'm trying to use matlab to solve a group of equations ( I want to find the values of x(1),x(2),x(3),x(4),x(5),and x(6)), I'm trying to use fsolve in *.m file because I need the results to be written directly for output file, could you please help me in that, the code that I use is between the two lines (but it always gives me an error):
> > -----------------------------------------------------------------------------------------------------------------------
> > function F = myfun(x)
> > x=zeros(3,2);
> > F(1)=inline('(x(1)^2)+74.201-(-17.228*x(1))+(x(2)^2)+5.341-(4.622*x(2))+(x(3)^2)+165.431-(25.724*x(3))-1398.760','x');
> > F(2)=inline('(x(4)^2)+74.201-(-17.228*x(4))+(x(5)^2)+5.341-(4.622*x(5))+(x(6)^2)+165.431-(25.724*x(6))-750.760','x');
> > F(3)=inline('(x(1)^2)+32.217-(-11.352*x(1))+(x(2)^2)+49.323-(-14.046*x(2))+(x(3)^2)+341.289-(36.948*x(3))-858.490','x');
> > F(4)=inline('(x(4)^2)+32.217-(-11.352*x(4))+(x(5)^2)+49.323-(-14.046*x(5))+(x(6)^2)+341.289-(36.948*x(6))-327.610','x');
> > F(5)=inline('(x(1)^2)+13.542-(7.360*x(1))+(x(2)^2)+68.013-(16.494*x(2))+(x(3)^2)+45.239-(13.452*x(3))-1608.010','x');
> > F(6)=inline('(x(4)^2)+13.542-(7.360*x(4))+(x(5)^2)+68.013-(16.494*x(5))+(x(6)^2)+45.239-(13.452*x(6))-912.040','x');
> > F(7)=inline('x(1)^2-(2*x(1)*x(4))+x(4)^2+x(2)^2-(2*x(2)*x(5))+x(5)^2+x(3)^2-(2*x(3)*x(6))+x(6)^2-224.101','x');
> > x0 = [1; 1; 1; 1; 1; 1];
> > options=optimset('Display','iter');
> > x=fsolve(F,x0,options);
> > end
> > ------------------------------------------------------------------------------------------------------------------------
> >
> > also I have a question about how can we get all the solutions for the above equations, because fsolve now gives just one solution, and I wonder if there is a method to produce all the solutions or if we can change the initial guess by using " for loop".
> >
> > any help is appreciated,
> > Thank you,
> > Ma'mon hatmal
> - - - - - - - - - -
> You are trying to solve seven different equations with only six unknowns. In general there would be no solution. Equations F(1), F(3), and F(5) represent the intersection between three spheres in x(1), x(2), and x(3) space and would have either two solutions, one solution, or none. Equations F(2), F(4), and F(6) give three spheres in x(4), x(5), and x(6) space with the same centers as in the other three spheres but with apparently different radii. The last equation F(7) is the assertion that the two solutions in the first set are a given distance from those in the second set. As you can see, this last would not be true in general. You should not be trying to solve all seven but only the three F(1), F(3), and F(5) together and then F(2), F(4), and F(6) together as a separate problem. Then you can use F(7) to check whether any combination from the other solutions satisfy it.
>
> If you are having trouble finding all solutions, you should know that there is a fairly direct way of solving the problem of the intersection of three spheres. It involves first finding where the line connecting the two solutions passes through the plane of the three centers, and next moving out along this line an appropriate distance.
>
> Roger Stafford

Hi Roger,
finding all the solutions is one of the problems, my main problem now is to use fsolve inside *.m file, I did that (as indicated in the above code) but I always receive an error, so I wonder if you have any idea how to correct the code to make fsolve work in *.m file.
thank you,
Ma'mon

Subject: MATLAB PROBLEM

From: Mamon

Date: 5 Sep, 2010 21:10:06

Message: 5 of 10

Walter Roberson <roberson@hushmail.com> wrote in message <F5Qgo.10060$u16.3062@newsfe17.iad>...
> On 05/09/10 7:33 AM, Mamon wrote:
> > Hi ALL ; I'm a PhD student at USC, I'm trying to use matlab to solve a
> > group of equations ( I want to find the values of
> > x(1),x(2),x(3),x(4),x(5),and x(6)),
>
> > also I have a question about how can we get all the solutions for the
> > above equations, because fsolve now gives just one solution, and I
> > wonder if there is a method to produce all the solutions or if we can
> > change the initial guess by using " for loop".
>
> I looked around a bit a couple of months ago to see if I could find any
> known upper bound on the number of zeros of a multinomial, but I was
> unsuccessful even in just two dimensions. I saw one argument sketched
> out that the number of solutions must be infinite (at least if you
> include imaginary solutions) even just for 2 variables.

Hi Walter,
you are right, this is one of the problems that I faced,because I don't know how many possible solutions for nonlinear equations.
my main problem now is to use fsolve inside *.m file, but it always gives me an error, so I wonder if you know how to correct the code to make fsolve work inside *.m file.
thank you,
Ma'mon

Subject: MATLAB PROBLEM

From: Marcus M. Edvall

Date: 5 Sep, 2010 21:47:32

Message: 6 of 10

Hi Ma'mon,

Try the attached code with TOMLAB:

x = tom('x',6,1);

resid = [(x(1)^2)+74.201-(-17.228*x(1))+(x(2)^2)+5.341-(4.622*x(2))+
(x(3)^2)+165.431-(25.724*x(3))-1398.760;
(x(4)^2)+74.201-(-17.228*x(4))+(x(5)^2)+5.341-(4.622*x(5))+
(x(6)^2)+165.431-(25.724*x(6))-750.760;
(x(1)^2)+32.217-(-11.352*x(1))+(x(2)^2)+49.323-(-14.046*x(2))+
(x(3)^2)+341.289-(36.948*x(3))-858.490;
(x(4)^2)+32.217-(-11.352*x(4))+(x(5)^2)+49.323-(-14.046*x(5))+
(x(6)^2)+341.289-(36.948*x(6))-327.610;
(x(1)^2)+13.542-(7.360*x(1))+(x(2)^2)+68.013-(16.494*x(2))+
(x(3)^2)+45.239-(13.452*x(3))-1608.010;
(x(4)^2)+13.542-(7.360*x(4))+(x(5)^2)+68.013-(16.494*x(5))+
(x(6)^2)+45.239-(13.452*x(6))-912.040;
x(1)^2-(2*x(1)*x(4))+x(4)^2+x(2)^2-(2*x(2)*x(5))+x(5)^2+x(3)^2-
(2*x(3)*x(6))+x(6)^2-224.101];

x0 = [1; 1; 1; 1; 1; 1];

options = struct;
options.norm = 'L2';
options.type = 'cls';
Prob = sym2prob(resid,[],{x == x0},options);
Prob.LS.y = zeros(7,1);
Result = tomRun('multiMin', Prob, 1);

Best wishes, Marcus
Tomlab Optimization Inc.
http://tomopt.com/
http://tomsym.com/

Subject: MATLAB PROBLEM

From: Roger Stafford

Date: 5 Sep, 2010 22:01:08

Message: 7 of 10

"Mamon " <mamoon_hatmal@yahoo.com> wrote in message <i610m0$9oh$1@fred.mathworks.com>...
> Hi Roger,
> finding all the solutions is one of the problems, my main problem now is to use fsolve inside *.m file, I did that (as indicated in the above code) but I always receive an error, so I wonder if you have any idea how to correct the code to make fsolve work in *.m file.
> thank you,
> Ma'mon
- - - - - - - - - - -
  I have already indicated to you why you are getting the error. The 'fsolve' function does not like to receive more equations than unknowns. It will continue to do so until you correct this aspect of your problem.

  I have also told you that you will get at most two solutions to the F(1), F(3), F(5) equations and at most two to the F(2), F(4), F(6) equations which makes a total of four possible combinations. Unless the F(7) equation is just right, it will fail for each of these four, which would mean there would be no solutions at all. What makes you believe that F(7) is compatible with the others?

  And finally I have told you there is a way of getting both solutions to a three-spheres intersection problem using straightforward analysis rather than using fsolve in case it gives you only one solution.

Roger Stafford

Subject: MATLAB PROBLEM

From: Mamon

Date: 5 Sep, 2010 22:53:05

Message: 8 of 10

"Marcus M. Edvall" <edvall@gmail.com> wrote in message <c160566d-58f1-44cc-b85b-2317c5c1bbc3@u6g2000yqh.googlegroups.com>...
> Hi Ma'mon,
>
> Try the attached code with TOMLAB:
>
> x = tom('x',6,1);
>
> resid = [(x(1)^2)+74.201-(-17.228*x(1))+(x(2)^2)+5.341-(4.622*x(2))+
> (x(3)^2)+165.431-(25.724*x(3))-1398.760;
> (x(4)^2)+74.201-(-17.228*x(4))+(x(5)^2)+5.341-(4.622*x(5))+
> (x(6)^2)+165.431-(25.724*x(6))-750.760;
> (x(1)^2)+32.217-(-11.352*x(1))+(x(2)^2)+49.323-(-14.046*x(2))+
> (x(3)^2)+341.289-(36.948*x(3))-858.490;
> (x(4)^2)+32.217-(-11.352*x(4))+(x(5)^2)+49.323-(-14.046*x(5))+
> (x(6)^2)+341.289-(36.948*x(6))-327.610;
> (x(1)^2)+13.542-(7.360*x(1))+(x(2)^2)+68.013-(16.494*x(2))+
> (x(3)^2)+45.239-(13.452*x(3))-1608.010;
> (x(4)^2)+13.542-(7.360*x(4))+(x(5)^2)+68.013-(16.494*x(5))+
> (x(6)^2)+45.239-(13.452*x(6))-912.040;
> x(1)^2-(2*x(1)*x(4))+x(4)^2+x(2)^2-(2*x(2)*x(5))+x(5)^2+x(3)^2-
> (2*x(3)*x(6))+x(6)^2-224.101];
>
> x0 = [1; 1; 1; 1; 1; 1];
>
> options = struct;
> options.norm = 'L2';
> options.type = 'cls';
> Prob = sym2prob(resid,[],{x == x0},options);
> Prob.LS.y = zeros(7,1);
> Result = tomRun('multiMin', Prob, 1);
>
> Best wishes, Marcus
> Tomlab Optimization Inc.
> http://tomopt.com/
> http://tomsym.com/

Hi Marcus,
thank you for the reply,I don't have tomlab, I just used the code directly into matlab and it gaves me the following error:
-------------------------------------------------------------------------------------------------------
??? Attempt to execute SCRIPT tom as a function:
C:\Users\Mamon\Desktop\tom.m

Error in ==> tom at 1
x = tom('x',6,1);
-------------------------------------------------------------------------------------------------------
I will buy tomlab online if the code above gives all the solutions, so I wonder how many soultions your code gave.
also I wonder if there is any method using matlab without tomlab because I saw some people solved similar problems online and I tried their code but it is not working.
thank you,
Ma'mon

Subject: MATLAB PROBLEM

From: Mamon

Date: 5 Sep, 2010 23:04:04

Message: 9 of 10

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i613v4$7ff$1@fred.mathworks.com>...
> "Mamon " <mamoon_hatmal@yahoo.com> wrote in message <i610m0$9oh$1@fred.mathworks.com>...
> > Hi Roger,
> > finding all the solutions is one of the problems, my main problem now is to use fsolve inside *.m file, I did that (as indicated in the above code) but I always receive an error, so I wonder if you have any idea how to correct the code to make fsolve work in *.m file.
> > thank you,
> > Ma'mon
> - - - - - - - - - - -
> I have already indicated to you why you are getting the error. The 'fsolve' function does not like to receive more equations than unknowns. It will continue to do so until you correct this aspect of your problem.
>
> I have also told you that you will get at most two solutions to the F(1), F(3), F(5) equations and at most two to the F(2), F(4), F(6) equations which makes a total of four possible combinations. Unless the F(7) equation is just right, it will fail for each of these four, which would mean there would be no solutions at all. What makes you believe that F(7) is compatible with the others?
>
> And finally I have told you there is a way of getting both solutions to a three-spheres intersection problem using straightforward analysis rather than using fsolve in case it gives you only one solution.
>
> Roger Stafford

Hi Roger,
I'm sorry because it is my mistake from beginning not to explain that enough.
I know the solution for these equations because I'm writing a code now and I'm trying to apply that for known cases first, so I know exactly the solution for that, fsolve is working inside the command window and gives me always a solution (which depends on the intial guess), but I have two problems:
1-make fsolve working inside *.m file.
2-if there is a method (e.g. for loop) that change the initial guess every time so we can have mutiple solutions.

the method that you have suggested (straightforward analysis ) is intersting so I will read about it, but is it called "straightforward analysis" analysis .
thank you,

Subject: MATLAB PROBLEM

From: Roger Stafford

Date: 7 Sep, 2010 17:44:23

Message: 10 of 10

"Mamon " <mamoon_hatmal@yahoo.com> wrote in message <i602m1$3v$1@fred.mathworks.com>...
> Hi ALL ;
> I'm a PhD student at USC, I'm trying to use matlab to solve a group of equations ( I want to find the values of x(1),x(2),x(3),x(4),x(5),and x(6)), I'm trying to use fsolve in *.m file because I need the results to be written directly for output file, could you please help me in that, the code that I use is between the two lines (but it always gives me an error):
> ........
> also I have a question about how can we get all the solutions for the above equations, because fsolve now gives just one solution, and I wonder if there is a method to produce all the solutions or if we can change the initial guess by using " for loop".
>
> any help is appreciated,
> Thank you,
> Ma'mon hatmal
- - - - - - -
  Ma'mon, it pains me to think of you using fsolve in the manner you have explained and on such an elementary problem. The following is a direct way of using matlab to compute the two intersections (if any) of three spheres.

  Let C1, C2, and C3 be three-element coordinate vectors of the spheres' centers and let r1, r2, and r3 be their respective radii. Then do this:

% Define three random spheres and radii
C1 = randn(1,3); C2 = randn(1,3); C3 = randn(1,3);
r1 = 2+rand; r2 = 2+rand; r3 = 2+rand;

% Find their intersection points
U = C2-C1;
V = C3-C1;
W = cross(U,V);
w2 = dot(W,W);
P = ((r1^2-r2^2+dot(U,U))*cross(V,W)-(r1^2-r3^2+dot(V,V))*cross(U,W))/(2*w2);
h2 = r1^2-dot(P,P);
if h2 >= 0
 Q = sqrt(h2)*W/sqrt(w2);
 I1 = C1+P+Q;
 I2 = C1+P-Q;
else
 fprintf('The spheres don''t intersect.\n')'
end

% Test
[norm(I1-C1)-r1,norm(I1-C2)-r2,norm(I1-C3)-r3;
 norm(I2-C1)-r1,norm(I2-C2)-r2,norm(I2-C3)-r3]

The points I1 and I2 are the two points of intersection.

  Note: The point C1+P above is the point I previously mentioned "where the line connecting the two solutions passes through the plane of the three centers." Q and -Q are vectors pointing from C1+P in opposite directions along the line orthogonal to the centers' plane by the appropriate distance.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us