Using the Solve function with Sine and Cosine and Multiple Defined Variables

Hello Everyone,
I'm trying to solve for 'beta' in the following equation: (-b*cos(beta)+a-d*cos(alpha))^2+(d*sin(beta)-b*sin(alpha))^2=c^2
I have defined a, b, c, d, and 'alpha' and I'm trying to solve for 'beta'. Here's the Matlab code that I used, I can't seem to get it to output the betas for every iteration of alpha.
I wanted to create a list of betas that are generated. I've been trying very hard and I can't seem to find the solution. :(
Please help me if you can. Thanks in advance!
clc
clear
b=9.25;
d=14;
a=13;
c=11;
%range of alpha
alpha=(0.676315085:.01:1.40935337);
%solving for beta
syms beta
solve('(-b*cos(beta)+a-d*cos(alpha))^2+(d*sin(beta)-b*sin(alpha))^2=c^2','beta')

답변 (2개)

Change the last line to
S = solve('(-b*cos(beta)+a-d*cos(alpha))^2+(d*sin(beta)-b*sin(alpha))^2=c^2','beta');
nalpha = length(alpha);
betas = cell(nalpha,1);
for K = 1 : nalpha
betas{K} = double(subs(S,'alpha',alpha(K)));
end
You might notice here that I did not assume that there are the same number of outputs for each iteration. Each solution involves an arctan() of the roots of a quartic (order 4) polynomial, and I cannot promise that MuPad will always return 4 values if there happens to be a double root.
Expect to get mostly complex outputs. Likely even entirely complex outputs. Were you expecting that?

댓글 수: 8

With those particular constants, there are two real-valued solutions and two complex-valued solutions over that range of alpha.
There are sub-polynomials involved that are order cos(alpha)^12, with the sub-polynomial being raised to 1/2, then to 1/3, then to 1/2 . That is a series of opportunities for complex values to arise, and then question then becomes whether one can be _certain_ that the complex components will always cancel out. The answer is NO, not with those particular constants: you are going to get either 2 or 4 complex-valued results. There is nothing in the original question that restricts beta to being real-valued or non-negative.
i deleted the last line of the .mfile and i couldn't get it to work. it said:
??? Error using ==> solve>getEqns at 182
' (-b*cos(beta)+a-d*cos(alpha)).^2+(d*sin(beta)-b*sin(alpha)).^2=c^2 ' is not a valid
expression or equation.
Error in ==> solve at 67
[eqns,vars] = getEqns(varargin{:});
Error in ==> newworkingalternate at 23
S =
solve('(-b*cos(beta)+a-d*cos(alpha)).^2+(d*sin(beta)-b*sin(alpha)).^2=c^2','beta');
You changed your expression from what you had before. Before you had
(-b*cos(beta)+a-d*cos(alpha))^2+(d*sin(beta)-b*sin(alpha))^2=c^2
and now you have
(-b*cos(beta)+a-d*cos(alpha)).^2+(d*sin(beta)-b*sin(alpha)).^2=c^2
which uses .^ instead of ^
Go back to using plain ^ as the symbolic toolbox does not know any .^ operator.
I've tried it that way also and I still got an error. I don't really understand why I'm getting an error still. Can you post the addition of your solution with the stuff I had before in its entire form? Maybe I'm missing a few things. I just changed the last line to what you suggested. Thanks in advance! I really appreciate the help. It's a part of my senior design project that I can't quite figure out. All I need is to solve for this mysterious 'beta' and I can continue with the project. Thank you so much!
clc
clear
b=9.25;
d=14;
a=13;
c=11;
%range of alpha
alpha=(0.676315085:.01:1.40935337);
%solving for beta
syms beta
S = solve('(-b*cos(beta)+a-d*cos(alpha))^2+(d*sin(beta)-b*sin(alpha))^2=c^2','beta');
nalpha = length(alpha);
betas = cell(nalpha,1);
for K = 1 : nalpha
betas{K} = double(subs(S,'alpha',alpha(K)));
end
I inputed that into Matlab and I still got an error last night. I will definitely try again around noon & let you know my results. How does the matrix look like when it's solved? Just an nx1 with a bunch of solutions? Can you post the output?
I tried the code again and I got the same error. Nothing changed after I removed the "." from the equation.
To confirm: even though the string you are passing does not include any "." characters, the error message it generates for you does have the ^ changed to .^ ?? That would be very odd indeed.
I just realized that for your purposes, I missed a step in the solution: try changing the S = line to
S = solve( subs('(-b*cos(beta)+a-d*cos(alpha))^2+(d*sin(beta)-b*sin(alpha))^2=c^2',{'a', 'b', 'c', 'd'}, {a,b,c,d}), 'beta');
What the matrix looks like to me when it is solved is:
arctan((-.4263996139* RootOf(3122289*_Z^4 + (-14644896*cos(alpha)+13598832)*_Z^3 + (-11310208*cos(alpha)+13345058-6244578*cos(alpha)^2)*_Z^2 + (-40581008+88524128*cos(alpha)+14644896*cos(alpha)^3-61868144*cos(alpha)^2)*_Z + 3122289*sin(alpha)^4-20582016*cos(alpha)^3-61419904*cos(alpha)+7509504+75971072*cos(alpha)^2)^2 + (-.9285714285+.9999999999*cos(alpha)) * RootOf(3122289*_Z^4 + (-14644896*cos(alpha)+13598832)*_Z^3 + (-11310208*cos(alpha)+13345058-6244578*cos(alpha)^2)*_Z^2 + (-40581008+88524128*cos(alpha)+14644896*cos(alpha)^3-61868144*cos(alpha)^2)*_Z + 3122289*sin(alpha)^4-20582016*cos(alpha)^3-61419904*cos(alpha)+7509504+75971072*cos(alpha)^2)+1.272442085+.4263996139*cos(alpha)^2-1.405405405*cos(alpha))/sin(alpha), RootOf(3122289*_Z^4 + (-14644896*cos(alpha)+13598832)*_Z^3 + (-11310208*cos(alpha)+13345058-6244578*cos(alpha)^2)*_Z^2 + (-40581008+88524128*cos(alpha)+14644896*cos(alpha)^3-61868144*cos(alpha)^2)*_Z + 3122289*sin(alpha)^4-20582016*cos(alpha)^3-61419904*cos(alpha)+7509504+75971072*cos(alpha)^2))
But I am using Maple, not the Symbolic Toolbox.
The solution above can be converted to numeric MATLAB code as:
nalpha = length(alpha);
betas = cell(nalpha,1);
for K = 1 : nalpha
t1 = roots([3122289, (-0.14644896e8 * cos(alpha(K)) + 0.13598832e8), (-0.11310208e8 * cos(alpha(K)) + 0.13345058e8 - 0.6244578e7 * cos(alpha(K)) ^ 2), (-0.40581008e8 + 0.88524128e8 * cos(alpha(K)) + 0.14644896e8 * cos(alpha(K)) ^ 3 - 0.61868144e8 * cos(alpha(K)) ^ 2), + 0.3122289e7 * sin(alpha(K)) ^ 4 - 0.20582016e8 * cos(alpha(K)) ^ 3 - 0.61419904e8 * cos(alpha(K)) + 0.7509504e7 + 0.75971072e8 * cos(alpha(K)) ^ 2]);
t2 = t1 .^ 2;
t4 = cos(alpha(K));
t8 = t4 .^ 2;
t12 = sin(alpha(K));
betas{K} = atan2((-0.4263996139e0 .* t2 + (-0.9285714285e0 + 0.9999999999e0 .* t4) .* t1 + 0.1272442085e1 + 0.4263996139e0 .* t8 - 0.1405405405e1 .* t4) ./ t12, t1);
end
I have written this code to be vectorized over the four roots produced per alpha value, but it would need further extension to vectorize over all of the alpha values. There would, for example, be the difficulty that the numeric root finder, roots(), cannot be used over an array of coefficients.
To be honest, I would not use this numeric solution myself. I would use the general symbolic solution for the full formula without any constants having been substituted in, leaving the constants unsubstituted as long as practical. The roots of the quadratic are going to be fairly dependent upon the exact values of the constants, and substituting the constants in too early is going to result in significant loss of precision before you even take the roots of the quartic. You would, for example, get a considerable difference if you used b = sym('37/4') compared to using b = 9.25 as using the algebraic form would prevent the precision from being lost as early.

댓글을 달려면 로그인하십시오.

bym
bym 2011년 11월 1일
a different approach from Walter's:
alpha=(0.676315085:.01:1.40935337);
b=9.25;
d=14;
a=13;
c=11;
fx = zeros(numel(alpha),2);
fx(:,1) = alpha';
for ii = 1:numel(alpha)
y = alpha(ii);
f = @(x) (-b*cos(x)+a-d*cos(y)).^2+(d*sin(x)-b*sin(y)).^2-c^2;
fx(ii,2) = fzero(f,.75);
end
fx

댓글 수: 2

fzero() can only be used for real-valued functions, and it will only find a single solution. With those particular constants, are two real-valued solutions over the entire range of alpha.
I also did it this way and I ended up with positive and negative values which were really close to alpha & gave me a linear relationship. I think that there should also be a set of complex solutions also.

댓글을 달려면 로그인하십시오.

질문:

2011년 11월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by