Solving equations involving angles in matlab. Matrix or linsolve way - matlab

I cant get matlab to output an answer for me when I do x=A^-1*b or linsolve (A,b)
I have four equations and 4 variables to solve. It involves sin and cos.
ra=.15; r1=.15;r2=.1;rb=.15; //Radius
syms Wa Wb %weight
W1=45;W2=90;
syms tA tB %theta
t1=90;t2=0;
aA=.03;a1=.18;a2=.33;aB=.43; %radius
%Trying to solve for Wa,Wb,tA,tB
mrcos=Wa*ra*cos(tA) + W1*r1*cos(t1) + W2*r2*cos(t2) + Wb*rb*cos(tB)==0; %mrcos
mrsin=Wa*ra*sin(tA) + W1*r1*sin(t1) + W2*r2*sin(t2) + Wb*rb*sin(tB)==0; %mrsin
macos=Wa*aA*cos(tA) + W1*a1*cos(t1) + W2*a2*cos(t2) + Wb*aB*cos(tB)==0; %macos
masin=Wa*aA*sin(tA) + W1*a1*sin(t1) + W2*a2*sin(t2) + Wb*aB*sin(tB)==0; %masin
A=[Wa*ra*cos(tA) W1*r1*cos(t1) W2*r2*cos(t2) Wb*rb*cos(tB);
Wa*ra*sin(tA) W1*r1*sin(t1) W2*r2*sin(t2) Wb*rb*sin(tB);
Wa*aA*cos(tA) W1*a1*cos(t1) W2*a2*cos(t2) Wb*aB*cos(tB);
Wa*aA*sin(tA) W1*a1*sin(t1) W2*a2*sin(t2) Wb*aB*sin(tB)];
b=[0 0 0 0]';
x=inv(A)*b
it just gives me the following when im trying to solve for Wa,Wb,tA,tB
x=
[0
0
0
0]
im expecting not these values... ik they arent 0

Related

How to solve the following problem on MATLAB

a(x) here is a type of equation used in signal
Let
y(x) = x^3 + x^2 + x + 1
a(x) is inputed by the user so for example if user inputs
a(x) = D2 + D + 6
y(x)*a(x) = D2(x^3 + x^2 + x + 1) + D(x^3 + x^2 + x + 1) + 6(x^3 + x^2 + x + 1)
here D2(x^3 + x^2 + x + 1) = 6x + 2 and D(x^3 + x^2 + x + 1) = 3x^2 + 2x + 1
So D is differentiation and D2 is double differential
So i want to know how i would do something like this on MATLAB
Use Matlab symbolic computation toolbox https://www.mathworks.com/products/symbolic.html
Calculus
Evaluate exact analytical solutions for definite or indefinite integral,
calculate derivatives of symbolic expressions or functions,
and approximate functions using series expansions.

Find solutions of a system of equations as close as possible to the previous solutions

Currently I am using the vpasolve command to solve a system of 3 equations with 3 unknowns in it. These 3 equations are being used to model the kinematic motion of a robot manipulator, and the unknowns are either the lengths of the links of the robot or the angles of rotation of the joints. The vpasolve command manages to solve the equations correctly, the problem is that the solutions that I get are not close. What do I mean by not close? If I get a solution of 1.5, 0.3, -0.4 [rad] for one of the angles, the next solution for the next step of the motion would be any other random angle that may solve it, but in reality is not useful, so it could be something like -0.8 -1.5 2 [rad]. Of course it would be pointless for a robot to jump from 1.5 rad to -0.8 rad just to move its end point by 1 cm. Instead I would like to get a solution that is as close as possible to the angles from the previous solution.
The equations look something like this:
x == 1*cos(theta1)*cos(0) + 1*cos(theta1 + theta2)*cos(0 + 0) + 1*cos(theta1 + theta2 + theta3)*cos(0 + 0 + 0)
y == 1*cos(theta1)*sin(0) + 1*cos(theta1 + theta2)*sin(0 + 0) + 1*cos(theta1 + theta2 + theta3)*sin(0 + 0 + 0)
z == 1*sin(theta1) + 1*sin(theta1 + theta2) + 1*sin(theta1 + theta2 + theta3)
I give them different values for x y and z and solve the thetas for these values. For example:
t = 0:0.1:1;
x = 2 * t;
y = 0;
z = 1;
So here we have 10 different positions, I know that there are "consecutive" solutions, I just dont know how to make vpasolve give me these solutions, here is how I use it:
syms x y z theta1 theta2 theta3
xEquation = 'x == 1*cos(theta1)*cos(0) + 1*cos(theta1 + theta2)*cos(0 + 0) + 1*cos(theta1 + theta2 + theta3)*cos(0 + 0 + 0)';
yEquation = 'y == 1*cos(theta1)*sin(0) + 1*cos(theta1 + theta2)*sin(0 + 0) + 1*cos(theta1 + theta2 + theta3)*sin(0 + 0 + 0)';
zEquation = 'z == 1*sin(theta1) + 1*sin(theta1 + theta2) + 1*sin(theta1 + theta2 + theta3)';
t = 0:0.1:1;
x = 2 * t;
y = 0;
z = 1;
xEquationEv = eval(xEquation);
yEquationEv = eval(yEquation);
zEquationEv = eval(zEquation);
for f = 1:size(x, 2)
sol(f) = vpasolve([xEquationEv(f), yEquationEv, zEquationEv], [theta1, theta2, theta3], [-pi pi; -pi pi; -pi pi]);
end
sol.theta1
sol.theta2
sol.theta3
You will see how the values are not smoothly incrementing or decrementing all the time, but sometimes they make big jumps.
Note: I am starting off with strings and eval, because the equations may change, I have another algorithm that is generating them.
Sounds like you have already solved this, but you might also try using the unwrap function on your original result.
For non-polynomials, vpasolve only returns one answer (of two, in your case) to the equations. unwrap should tend to reduce the number of times that the variables jump between the different solutions to your equations, though you will need to reduce the default tolerance (as that was set up to catch 2*pi jumps).
I would try plotting unwrap(sol.theta1,pi/2), for example, and see if that looks more like what you were expecting.
I think I managed to solve it by recalculating the limits after obtaining the first solution. Something like:
limits = [previousTheta1 - step previousTheta1 + step;
previousTheta2 - step previousTheta2 + step;
previousTheta3 - step previousTheta3 + step];
That gave me much smoother motion (solutions).

How can I get MATLAB to numerically solve a particularly nasty system of equations?

I have four parameters, t1,t2, theta1, and theta2. I want to find a solution (there can potentially be infinitely many) to the following system of equations:
t1*cos(theta1) + t2*cos(theta2) + 2*t1*t2*sin(theta1 + theta2) = t1*sin(theta1) + t2*sin(theta2) + 2*t1*t2*cos(theta1 + theta2) + 1
t1*cos(theta1) + t2*cos(theta2) + t1*sin(theta1) + t2*sin(theta2) = 2*t1*t2*sin(theta1 + theta2) + 2*t1*t2*cos(theta1 + theta2) + 1
2*t1^2*t2^2 + sin(theta1)*t1*t2^2 + sin(theta1 + theta2)*t1*t2 + 1 = sin(theta2)*t1^2*t2 + t1^2 + sin(theta1)*t1 + t2^2
Since there are more parameters than equations, we have to impose further restrictions to identify a specific solution to this system. Right now I don't really care which solution is picked, as long as it isn't trivial (like all the variables equaling zero).
My current method is to set the third equation to 0.5, solving for t1 and t2 in terms of theta1 and theta2, and substituting these expressions into the first two equations to solve for theta1 and theta2. However, MATLAB is trying to do this symbolically, which is extremely time-consuming. Is there a way I can get some approximate solutions to this system? I can't exactly plot the surfaces represented by these equations and look at their intersection, because each side of the equations involves more than two parameters, which means it can't be visualized in three dimensions.
You can use fsolve to do this.
First, you need to create anonymous functions for the residuals of the three equations. I'm going to create a vector x where x = [t1; t2; theta1; theta2]. That makes the residuals:
r1 = #(x) x(1)*cos(x(3)) + x(2)*cos(x(4)) + 2*x(1)*x(2)*sin(x(3) + x(4)) - (x(1)*sin(x(3)) + x(2)*sin(x(4)) + 2*x(1)*x(2)*cos(x(3) + x(4)) + 1);
r2 = #(x) x(1)*cos(x(3)) + x(2)*cos(x(4)) + x(1)*sin(x(3)) + x(2)*sin(x(4)) - (2*x(1)*x(2)*sin(x(3) + x(4)) + 2*x(1)*x(2)*cos(x(3) + x(4)) + 1);
r3 = #(x) 2*x(1)^2*x(2)^2 + sin(x(3))*x(1)*x(2)^2 + sin(x(3) + x(4))*x(1)*x(2) + 1 - (sin(x(4))*x(1)^2*x(2) + x(1)^2 + sin(x(3))*x(1) + x(2)^2);
Then, we need to make a single function that is a vector of those three residuals, since fsolve tries to get this vector to be zero:
r = #(x) [r1(x); r2(x); r3(x)]
Now, we call fsolve. I picked an arbitrary starting point:
>> [x, fval, exitflag] = fsolve(r, [0.5,0.5,pi/4,pi/4])
Warning: Trust-region-dogleg algorithm of FSOLVE cannot handle non-square systems; using Levenberg-Marquardt algorithm instead.
> In fsolve at 287
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
x =
0.9654 0.5182 0.7363 0.7344
fval =
1.0e-10 *
-0.0090
0.2743
-0.0181
exitflag =
1
You can ignore the warning. x is the four values you were looking for. fval is the value of the residuals. exitFlag == 1 means we found a root.

limitations of non linear multivariant equation solvers in matlab

I have a system of non-linear multivariate equations. I am only interested in the roots of the system in an interval of each variable.
For example,
F = #(x,y,z) [...
sin((x*y)/100 + (y*z)/5)/10 + sin((y*z)/10)/10 + (y*cos((y*z)/10)*(x/10 + z))/100 + (y*cos((x*y)/100 + (y*z)/5)*(x/5 + z))/50 = 0
(z*cos((y*z)/10)*(x/10 + z))/100 + (cos((x*y)/100 + (y*z)/5)*(x/5 + z)*(x/100 + z/5))/10 = 0
sin((x*y)/100 + (y*z)/5)/50 + sin((y*z)/10)/100 + (y*cos((x*y)/100 + (y*z)/5)*(x/5 + z))/1000 = 0
];
and I am only interested in the interval of x in [0 10] and y in [-10 10] and z in [-5, 10].
My question is:
is there any method that can find 'all' the roots in these interval?
is there any solution that can tell me only number of roots in these intervals ?
If there is no general solution for question 1 and 2 (which I think there is not), then under what condition I can find all the roots (e.x. linear equations or polynomial are clearly solvable, but I want something more general such as Trigonometric equations).

How can I solve a system of 4 equations and 4 unknowns with MATLAB?

I have a general equation
t=tr+(ts-tr)/(1+(a*h)^n)^(1-1/n)
for (h=0, 1, 2, 3), I have t=2.000, 1.6300, 1.2311, 1.1084. therefor there are 4 equations with 4 unknowns tr, ts, a, n
I used "solve" function in matlab
s=solve('tr+(ts-tr)/(1+(a*0)^n)^(1-1/n)=2','tr+(ts-tr)/(1+(a*1)^n)^(1-1/n)=1.63','tr+(ts-tr)/(1+(a*2)^n)^(1-1/n)=1.2311','tr+(ts-tr)/(1+(a*3)^n)^(1-1/n)=1.1084')
and error is
??? Error using ==> mupadmex
Error in MuPAD command: Singularity [ln];
during evaluation of 'numeric::fsolve'
Error in ==> sym.sym>sym.mupadmexnout at 2018
out = mupadmex(fcn,args{:});
Error in ==> solve at 76
[symvars,R] = mupadmexnout('symobj::solvefull',eqns,vars);
What should I do?
The problem appears with you using the solve function. That only works for simple equations, it is better to use the fsolve function. Due to the fact that I am worried that I am doing an assignment for you, I am only going to show you how to do another example using fsolve.
Suppose that you want to solve
1 = x_1
1 = x_1 + x_2
-1 = x_1 + x_2 + x_3
-1 = x_1 + x_2 + x_3 + x_4
then what you firstly need to do is write these as equations equal 0
0 = x_1 - 1
0 = x_1 + x_2 - 1
0 = x_1 + x_2 + x_3 + 1
0 = x_1 + x_2 + x_3 + x_4 + 1
then you need to write a function that takes in a vector x, the components of x will represent x_1, x_2, x_3 and x_4. The output of the function will also be a vector whose components should the outputs of the Right hand side of the above equations (see the function fun below). This function is going to be called by fSolve for it to provide it with guesses of the correct value of x, until it guess correct. When never actually run this function ourselves. That is why it is below the top function.
Then you create a function handle to this function by fHandle = #fun. You can think of fHandle as another name for fun, when we calculate fHandle([1; 2; 3; 4]) this is the same as calculating fun([1; 2; 3; 4]). After this you make an initial guess of the correct vector x, say we chose xGuess = [1; 1; 1; 1]. Finally we pass fHandle and xGuess to fSolve.
Here is the code
function Solve4Eq4Unknown()
fHandle = #fun;
xGuess = ones(4,1);
xSolution = fsolve(fHandle, xGuess)
end
function y = fun(x)
y = zeros(4,1); % This step is not necessary, but it is effecient
y(1) = x(1) - 1;
y(2) = x(1) + x(2) - 1;
y(3) = x(1) + x(2) + x(3) + 1;
y(4) = x(1) + x(2) + x(3) + x(4) + 1;
end