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개)
Walter Roberson
2011년 11월 1일
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
Walter Roberson
2011년 11월 2일
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.
Mike
2011년 11월 2일
Walter Roberson
2011년 11월 2일
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.
Mike
2011년 11월 2일
Walter Roberson
2011년 11월 2일
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
Mike
2011년 11월 2일
Mike
2011년 11월 2일
Walter Roberson
2011년 11월 2일
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
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
Walter Roberson
2011년 11월 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.
Mike
2011년 11월 2일
카테고리
도움말 센터 및 File Exchange에서 Common Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!