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

# Thread Subject: Solving nonlinear equation with constraint

 Subject: Solving nonlinear equation with constraint From: hinhthoi Date: 19 Aug, 2012 09:41:05 Message: 1 of 27 I'm trying to solve an equation with a 'non-ordinary' constraint over the past few days but cannot find the solution The equation is: 0.155*4.45*cos(4.45*x) +0.155*sin(4.45*x) - 0.155*sin(4.45*0.3) - 0.155*4.45*cos(4.45*x)*(x+1-0.3) = 0 the constraint is: abs(0.155*4.45*cos(4.45*x)) is minimum. Theoretically, the first equation has many solutions, but i want to find x such that the constraint is satisfied. Could anyone please help me with this? Thanks!
 Subject: Solving nonlinear equation with constraint From: Bruno Luong Date: 19 Aug, 2012 10:38:06 Message: 2 of 27 Is it a home work? All I can tell you is that the solution does not exists, i.e., there is an infinity of solutions of the first equation, but none of them is the minimum of the constraint written in the second equation. Bruno
 Subject: Solving nonlinear equation with constraint From: hinhthoi Date: 19 Aug, 2012 10:53:06 Message: 3 of 27 "Bruno Luong" wrote in message ... > Is it a home work? > > All I can tell you is that the solution does not exists, i.e., there is an infinity of solutions of the first equation, but none of them is the minimum of the constraint written in the second equation. > > Bruno Hi Bruno, It is one part of my project but i got stick at this step. It is not a student homework. I'm not sure but i feel that this problem is doable. first equation has an infinite of solutions, but in all matlab solver i need an initial guess. I'm not sure if i understand correctly "none of them is the minimum of the constraint written in the second equation", but within many solutions, i want to pick the solution that satisfies the constraint equation. I know i can do many fzero with many initial guesses and substitude x into the second equation and compare the results and pick the x value that the second equation is the minimum amongst all x values
 Subject: Solving nonlinear equation with constraint From: hinhthoi Date: 19 Aug, 2012 10:55:07 Message: 4 of 27 but i'm looking for a smarter and more rigorous method
 Subject: Solving nonlinear equation with constraint From: Bruno Luong Date: 19 Aug, 2012 10:59:09 Message: 5 of 27 "hinhthoi " wrote in message ... > > I know i can do many fzero with many initial guesses and substitude x into the second equation and compare the results and pick the x value that the second equation is the minimum amongst all x values I repeat: none of them is a minimum. You can pick any solution of the first equation, there is always other that get smaller value in the second equation. Bruno
 Subject: Solving nonlinear equation with constraint From: hinhthoi Date: 19 Aug, 2012 11:15:07 Message: 6 of 27 "Bruno Luong" wrote in message ... > "hinhthoi " wrote in message ... > > > > > I know i can do many fzero with many initial guesses and substitude x into the second equation and compare the results and pick the x value that the second equation is the minimum amongst all x values > > I repeat: none of them is a minimum. You can pick any solution of the first equation, there is always other that get smaller value in the second equation. > > Bruno Maybe you didn't understand exactly my question. The question is NOT to find the x for the second equation so that the second equation is minimum. It is to selecting one of the solutions (x1, x2, x3, ...) in the first equation (let say, solution x1) so that: cos(4.45*x1) < cos(4.45*x2) cos(4.45*x1) < cos(4.45*x3) cos(4.45*x1) < cos(4.45*x4) cos(4.45*x1) < cos(4.45*x5) ... and so on Hope my question is clear enough now
 Subject: Solving nonlinear equation with constraint From: Bruno Luong Date: 19 Aug, 2012 11:50:07 Message: 7 of 27 "hinhthoi " wrote in message ... > Maybe you didn't understand exactly my question. The question is NOT to find the x for the second equation so that the second equation is minimum. It is to selecting one of the solutions (x1, x2, x3, ...) in the first equation (let say, solution x1) so that: > > cos(4.45*x1) < cos(4.45*x2) > cos(4.45*x1) < cos(4.45*x3) > cos(4.45*x1) < cos(4.45*x4) > cos(4.45*x1) < cos(4.45*x5) > ... and so on > > Hope my question is clear enough now In contrary I understand your question. Actually I'm sure you don't understand what I wrote: no such x1 exists. Bruno
 Subject: Solving nonlinear equation with constraint From: hinhthoi Date: 19 Aug, 2012 12:16:06 Message: 8 of 27 "Bruno Luong" wrote in message ... > "hinhthoi " wrote in message ... > > > Maybe you didn't understand exactly my question. The question is NOT to find the x for the second equation so that the second equation is minimum. It is to selecting one of the solutions (x1, x2, x3, ...) in the first equation (let say, solution x1) so that: > > > > cos(4.45*x1) < cos(4.45*x2) > > cos(4.45*x1) < cos(4.45*x3) > > cos(4.45*x1) < cos(4.45*x4) > > cos(4.45*x1) < cos(4.45*x5) > > ... and so on > > > > Hope my question is clear enough now > > In contrary I understand your question. Actually I'm sure you don't understand what I wrote: no such x1 exists. > > Bruno Well, i tried with different initial guesses on equation 1 (initial guess varies from -0.7 to 0.5 with 0.1 increment). Here is the results for equation 1: There are 3 solutions with the above initials guesses: x1 = -0.14657, x2 = -1.0579, x3 = 0.9003). Now, i know x1 satisfies the constraint in equation 2 because abs(x1) < abs(x2) abs(x1)
 Subject: Solving nonlinear equation with constraint From: Bruno Luong Date: 19 Aug, 2012 12:27:06 Message: 9 of 27 > Remember! "Minimum" term in equation 2 is not an absolute minimum, it is only relative minimum (comparing all solutions x1, x2, x3... in the first equation) What is "relative minimum"? Remember the first equation has an infinity solution. The second equation abs(cos(...)) will tends to zezo but never reach it. Similar to the series { 1, 1/2, 1/3 ... 1/n .. }. It never reach zero, and none of the element of the series is the minimum of the series. Bruno
 Subject: Solving nonlinear equation with constraint From: Matt J Date: 19 Aug, 2012 12:28:07 Message: 10 of 27 "hinhthoi " wrote in message ... > > The equation is: > 0.155*4.45*cos(4.45*x) +0.155*sin(4.45*x) - 0.155*sin(4.45*0.3) - 0.155*4.45*cos(4.45*x)*(x+1-0.3) = 0 > > the constraint is: > > abs(0.155*4.45*cos(4.45*x)) is minimum. > > Theoretically, the first equation has many solutions, but i want to find x such that the constraint is satisfied. =================== Your terminology is unconventional, making discussion a bit confusing. Basically, it sounds like you have a constrained optimization problem where the first equation (not the second) is the constraint: minimize cos(4.45*x)^2 %instead of non-differentiable abs(0.155*4.45*cos(4.45*x)) subject to the constraint 0.155*4.45*cos(4.45*x) +0.155*sin(4.45*x) - 0.155*sin(4.45*0.3) - 0.155*4.45*cos(4.45*x)*(x+1-0.3) = 0 If you have the optimization toolbox, you could try to solve this with FMINCON. I'm not sure it will work though since the constraints form a disconnected set.
 Subject: Solving nonlinear equation with constraint From: Bruno Luong Date: 19 Aug, 2012 12:29:06 Message: 11 of 27 "hinhthoi " wrote in message ... > > Now, i know x1 satisfies the constraint in equation 2 because > abs(x1) < abs(x2) > abs(x1) Where is the cos() term? Why do you change the problem in the middle? Bruno
 Subject: Solving nonlinear equation with constraint From: Bruno Luong Date: 19 Aug, 2012 12:37:07 Message: 12 of 27 "Matt J" wrote in message ... > "hinhthoi " wrote in message > > If you have the optimization toolbox, you could try to solve this with FMINCON. I'm not sure it will work though since the constraints form a disconnected set. Don't waste time. The solution does not even exist. And no, FMINCON is not designed to handle disconnected set. The problem here is closer to integer programming, which is entirely different than the class of problems FMINCON can handle. Bruno
 Subject: Solving nonlinear equation with constraint From: hinhthoi Date: 19 Aug, 2012 12:53:06 Message: 13 of 27 "Matt J" wrote in message ... > "hinhthoi " wrote in message ... > > > > The equation is: > > 0.155*4.45*cos(4.45*x) +0.155*sin(4.45*x) - 0.155*sin(4.45*0.3) - 0.155*4.45*cos(4.45*x)*(x+1-0.3) = 0 > > > > the constraint is: > > > > abs(0.155*4.45*cos(4.45*x)) is minimum. > > > > Theoretically, the first equation has many solutions, but i want to find x such that the constraint is satisfied. > =================== > > Your terminology is unconventional, making discussion a bit confusing. Basically, it sounds like you have a constrained optimization problem where the first equation (not the second) is the constraint: > > minimize cos(4.45*x)^2 %instead of non-differentiable abs(0.155*4.45*cos(4.45*x)) > > subject to the constraint > > 0.155*4.45*cos(4.45*x) +0.155*sin(4.45*x) - 0.155*sin(4.45*0.3) - 0.155*4.45*cos(4.45*x)*(x+1-0.3) = 0 > > If you have the optimization toolbox, you could try to solve this with FMINCON. I'm not sure it will work though since the constraints form a disconnected set. Hi Matt, I thought the second equation is actually an inequality, not a normal equation to solve so i put it as the constraint. I will read the fmincon now. Thanks
 Subject: Solving nonlinear equation with constraint From: hinhthoi Date: 19 Aug, 2012 12:57:07 Message: 14 of 27 "Bruno Luong" wrote in message ... > "hinhthoi " wrote in message ... > > > > > Now, i know x1 satisfies the constraint in equation 2 because > > abs(x1) < abs(x2) > > abs(x1) > > > Where is the cos() term? Why do you change the problem in the middle? > > Bruno No, i didn't change anything
 Subject: Solving nonlinear equation with constraint From: Bruno Luong Date: 19 Aug, 2012 13:01:09 Message: 15 of 27 "hinhthoi " wrote in message ... > > No, i didn't change anything Where is the cos() term then? Bruno
 Subject: Solving nonlinear equation with constraint From: hinhthoi Date: 19 Aug, 2012 13:13:07 Message: 16 of 27 "Bruno Luong" wrote in message ... > "hinhthoi " wrote in message ... > > > > > No, i didn't change anything > > Where is the cos() term then? > > Bruno The cos() is in both equation 1 and the inequality (that i called constraint)
 Subject: Solving nonlinear equation with constraint From: Bruno Luong Date: 19 Aug, 2012 13:27:06 Message: 17 of 27 "hinhthoi " wrote in message > The cos() is in both equation 1 and the inequality (that i called constraint) But why I don't see it towards the end of your post #8? What is the point of comparing abs(x1) with abs(x2) and abs(x3)? Bruno
 Subject: Solving nonlinear equation with constraint From: hinhthoi Date: 19 Aug, 2012 13:44:06 Message: 18 of 27 "Bruno Luong" wrote in message ... > "hinhthoi " wrote in message > > The cos() is in both equation 1 and the inequality (that i called constraint) > > But why I don't see it towards the end of your post #8? What is the point of comparing abs(x1) with abs(x2) and abs(x3)? > > Bruno I also don't know why you did not see it. It was written in my question (first post). this problem is one part of my project and the comparison of abs(x1), abs(x2) ... is useful for my application. Without selecting the right x (to satisfy constraint), i face a serious problem with the final result. I think you are right, fmincon cannot be used in this situation. But i still hope there is some way to solve it because i think the problem is perfectly valid
 Subject: Solving nonlinear equation with constraint From: hinhthoi Date: 19 Aug, 2012 14:16:06 Message: 19 of 27 "Bruno Luong" wrote in message ... > "hinhthoi " wrote in message > > The cos() is in both equation 1 and the inequality (that i called constraint) > > But why I don't see it towards the end of your post #8? What is the point of comparing abs(x1) with abs(x2) and abs(x3)? > > Bruno Oops, there were typo error in post #8 So i got 3 values x1, x2, and x3 abs(cos(4.45*x2)) < abs(cos(4.45*x1)) abs(cos(4.45*x2)) < abs(cos(4.45*x3)) so i will select x2 as the final solution. That was what i meant.
 Subject: Solving nonlinear equation with constraint From: hinhthoi Date: 19 Aug, 2012 14:18:06 Message: 20 of 27 "hinhthoi " wrote in message ... > "Bruno Luong" wrote in message ... > > "hinhthoi " wrote in message > > > The cos() is in both equation 1 and the inequality (that i called constraint) > > > > But why I don't see it towards the end of your post #8? What is the point of comparing abs(x1) with abs(x2) and abs(x3)? > > > > Bruno > I also don't know why you did not see it. It was written in my question (first post). > > this problem is one part of my project and the comparison of abs(x1), abs(x2) ... is useful for my application. Without selecting the right x (to satisfy constraint), i face a serious problem with the final result. > > I think you are right, fmincon cannot be used in this situation. But i still hope there is some way to solve it because i think the problem is perfectly valid I editted the typo again, it should be abs(cos(4.45*x1)), abs(cos(4.45*x2)), ... NOT abs(x1), abs(x2)... I want to bang my head
 Subject: Solving nonlinear equation with constraint From: Bruno Luong Date: 19 Aug, 2012 15:17:07 Message: 21 of 27 "hinhthoi " wrote in message ... > i think the problem is perfectly valid May be, but a valid without solution. Let's try something for you to see it. Please execute this code: f = @(x) 0.155*4.45*cos(4.45*x) +0.155*sin(4.45*x) - 0.155*sin(4.45*0.3) - 0.155*4.45*cos(4.45*x)*(x+1-0.3); for k=[1 10 100 1000]     x = fzero(@(y) cos(y) + (sin(y)-sin(1.335))./(1.335-y),pi/2+k*pi)/4.45;     fx = f(x);     fprintf('x=%g, fx=%f, gx=%f\n', x, fx, abs(0.155*4.45*cos(4.45*x))) end %% End code It give the following result: x=0.9003, fx=-0.000000, gx=0.447534 x=7.41255, fx=0.000000, gx=0.000603 x=70.9506, fx=-0.000000, gx=0.000061 x=706.329, fx=-0.000000, gx=0.000006 The program finds a bigger and bigger solution of the first equation, yet the abs(cos()) term that goes to 0 but never reach it. There is no solution that minimizes the abs(cos()) term, since it is suffice to select a larger solution that gives an even smaller abs(cos()) term. You chase something that goes away. Bruno
 Subject: Solving nonlinear equation with constraint From: Matt J Date: 19 Aug, 2012 17:32:07 Message: 22 of 27 "Bruno Luong" wrote in message ... > > It give the following result: > > x=0.9003, fx=-0.000000, gx=0.447534 > x=7.41255, fx=0.000000, gx=0.000603 > x=70.9506, fx=-0.000000, gx=0.000061 > x=706.329, fx=-0.000000, gx=0.000006 > > The program finds a bigger and bigger solution of the first equation, yet the abs(cos()) term that goes to 0 but never reach it. > > There is no solution that minimizes the abs(cos()) term, since it is suffice to select a larger solution that gives an even smaller abs(cos()) term. You chase something that goes away. ============== Although, if the original purpose of all this was just to find out the greatest lower bound on the abs(cos(...)) function, I guess we can safely conclude that it is zero.
 Subject: Solving nonlinear equation with constraint From: hinhthoi Date: 19 Aug, 2012 17:48:07 Message: 23 of 27 "Bruno Luong" wrote in message ... > "hinhthoi " wrote in message ... > > i think the problem is perfectly valid > > May be, but a valid without solution. > > Let's try something for you to see it. Please execute this code: > > f = @(x) 0.155*4.45*cos(4.45*x) +0.155*sin(4.45*x) - 0.155*sin(4.45*0.3) - 0.155*4.45*cos(4.45*x)*(x+1-0.3); > for k=[1 10 100 1000] > x = fzero(@(y) cos(y) + (sin(y)-sin(1.335))./(1.335-y),pi/2+k*pi)/4.45; > fx = f(x); > fprintf('x=%g, fx=%f, gx=%f\n', x, fx, abs(0.155*4.45*cos(4.45*x))) > end > > %% End code > > It give the following result: > > x=0.9003, fx=-0.000000, gx=0.447534 > x=7.41255, fx=0.000000, gx=0.000603 > x=70.9506, fx=-0.000000, gx=0.000061 > x=706.329, fx=-0.000000, gx=0.000006 > > The program finds a bigger and bigger solution of the first equation, yet the abs(cos()) term that goes to 0 but never reach it. > > There is no solution that minimizes the abs(cos()) term, since it is suffice to select a larger solution that gives an even smaller abs(cos()) term. You chase something that goes away. > > Bruno I see the issue now, you are so smart. Thank you so much! (even though i need to modify my project, so sad) By the way, i'm curious to know, if the constraint is to minimize abs(x), is it doable? Could you give a suggestion for that. That is quite interesting to learn. Thanks!
 Subject: Solving nonlinear equation with constraint From: Matt J Date: 19 Aug, 2012 18:03:06 Message: 24 of 27 "hinhthoi " wrote in message ... > > By the way, i'm curious to know, if the constraint is to minimize abs(x), is it doable? Could you give a suggestion for that. That is quite interesting to learn. =========== You could run fzero and when it finds a solution, x0, rerun it in an interval [0,x1] where x1
 Subject: Solving nonlinear equation with constraint From: Matt J Date: 19 Aug, 2012 18:13:07 Message: 25 of 27 "Matt J" wrote in message ... > "hinhthoi " wrote in message ... > > > > By the way, i'm curious to know, if the constraint is to minimize abs(x), is it doable? Could you give a suggestion for that. That is quite interesting to learn. > =========== > > You could run fzero and when it finds a solution, x0, rerun it in an interval [0,x1] where x1
 Subject: Solving nonlinear equation with constraint From: Bruno Luong Date: 19 Aug, 2012 20:29:06 Message: 26 of 27 "hinhthoi " wrote in message ... > > By the way, i'm curious to know, if the constraint is to minimize abs(x), is it doable? Could you give a suggestion for that. That is quite interesting to learn. Matt's suggestion is right on. I don't have anything to add. Bruno
 Subject: Solving nonlinear equation with constraint From: hinhthoi Date: 20 Aug, 2012 04:44:07 Message: 27 of 27 "Bruno Luong" wrote in message ... > "hinhthoi " wrote in message ... > > > > By the way, i'm curious to know, if the constraint is to minimize abs(x), is it doable? Could you give a suggestion for that. That is quite interesting to learn. > > Matt's suggestion is right on. I don't have anything to add. > > Bruno Okay, thank Bruno and Matt :-)