Solving trigonometric equations in MATLAB
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hi there,
I am trying to solve these two equations;
r1.cos(x1)=r2+r3.cos(x2)+r4.cos(x3)
r1.sin(x1)=r5+r3.sin(x2)+r4.sin(x3)
r1,r2,r3,r4,r5 and x1 are known, x2 and x3 are the unknowns. I need to obtain x2 and x3 values according to changing x1 value (between 100 and 126 degree).
I am a begginner for MATLAB, could you please help me out?
Thanks in advance.
採用された回答
Birdman
2020 年 3 月 24 日
Try the following code. It should help you:
r1=1;r2=2;r3=3;r4=4;r5=5;%random values
x1=100:1:126;
syms x2 x3
for i=1:numel(x1)
eq1=r1*cos(x1(i))==r2+r3*cos(x2)+r4*cos(x3);
eq2=r1*sin(x1(i))==r5+r3*sin(x2)+r4*sin(x3);
sol(i)=vpasolve([eq1 eq2],[x2 x3]);
end
The solutions are stored in sol variable. You can reach them by typing
sol(1).x2
sol(1).x3
sol(2).x2
sol(2).x3
.
.
and so on.
12 件のコメント
Kubilay AKPINAR
2020 年 3 月 24 日
This gives the answes in radians right? If I want to work on degree I must change cos or sin codes with cosd sind, am I correct?
Birdman
2020 年 3 月 24 日
Yes, exactly. Or simply you can multiply the outcomes with 180 and then divide them to pi.
Kubilay AKPINAR
2020 年 3 月 24 日
Thanks for your reply, but I don't understand how this code works. For example, how many values does x2 have? Because it yields different values when I enter sol(1).x2 or sol(2)x2. Could you please clarify that?
Birdman
2020 年 3 月 24 日
The changing values of x2 and x3 is dependent on x1. For instance, when x1=100, you have a pair of x2 and x3. When x1=101, you have another pair of x2 and x3 and so forth.
Kubilay AKPINAR
2020 年 3 月 24 日
Thank you very much, I have one more question, are these anwers correct? I mean, are they approximate answers or exact anwers? I am asking that because there are 4 unknowns (cosx2 sinx2 cosx3 sinx3) but 2 equations. According to mathematical rules you can not solve this problem in a normal way.
Birdman
2020 年 3 月 24 日
No, there are two unknowns and two equations. You can solve automatically. If it had been as you said, then MATLAB could not have solved this pair of equations. cos and sin are functions, do not get confused.
Kubilay AKPINAR
2020 年 3 月 24 日
Okey, thank you for your explaination and effort.
Birdman
2020 年 3 月 24 日
You are welcome.
Kubilay AKPINAR
2020 年 3 月 24 日
Hello again,
I found a chance to try your codes with the known values (for example, r1=7.25 etc.) but I am having an error.
Here it is the code;
r1=7.25; r2=1.85; r3=5.79; r4=1.43; r5=3.45; %random values
x1=100:1:126;
syms x2 x3
for i=1:numel(x1)
eq1=r1*cos(x1(i))==r2+r3*cos(x2)+r4*cos(x3);
eq2=r1*sin(x1(i))==r5+r3*sin(x2)+r4*sin(x3);
sol(i)=vpasolve([eq1 eq2],[x2 x3]);
end
And this is the answer;
>> sol(1).x2
ans =
Empty sym: 0-by-1
>> sol(1)
ans =
struct with fields:
x2: [0×1 sym]
x3: [0×1 sym]
>>
Could you please help me?
Birdman
2020 年 3 月 25 日
Hello,
If there is no solution found, then it means for r1=7.25 and x1=100, there is no numerical solution. Instead, you can try symbolic approach with solve command as follows:
r1=7.25; r2=1.85; r3=5.79; r4=1.43; r5=3.45; %random values
x1=100:1:126;
syms x2 x3
for i=1:numel(x1)
eq1=r1*cos(x1(i))==r2+r3*cos(x2)+r4*cos(x3);
eq2=r1*sin(x1(i))==r5+r3*sin(x2)+r4*sin(x3);
sol(i)=solve([eq1 eq2],[x2 x3]);
end
Then, by typing
vpa(sol(1).x2)
vpa(sol(1).x3)
.
.
and the same for the rest of the solutions, you can obtain the results. The results may be in imaginary form.
Kubilay AKPINAR
2020 年 3 月 26 日
編集済み: Kubilay AKPINAR
2020 年 3 月 26 日
Hello again,
I am sorry for late response but your codes work perfectly, it was my fault, I have correct answers now. However, I am continuing to solve other problems just like the one I posted here by using your way but I am having another problem.
This is my code;
format short g
%Angle
ro2b=7.25; ro2d=1.85; ro4a=5.79; rab=1.43; ro4d=3.45;
th2=100:126;
syms x2 x3
th4=zeros(1,27);
th3=zeros(1,27);
for i=1:numel(th2)
eq1=ro2b*cosd(th2(i))==ro2d+ro4a*cosd(x2)+rab*cosd(x3);
eq4=ro2b*sind(th2(i))==ro4d+ro4a*sind(x2)+rab*sind(x3);
sol(i)=vpasolve([eq1 eq4],[x2 x3]);
end
for j=1:27
th4(1,j)=sol(j).x2;
th3(1,j)=sol(j).x3;
end
%Velocity
w2=4;
syms w3 w4
for t=1:numel(th2)
eq3=-ro2b*sind(th2(t))*w2==-ro4a*sind(th4(t,1))*w4-rab*sind(th3(t,1))*w3;
eq4=ro2b*cosd(th2(t))*w2==ro4a*cosd(th4(t,1))*w4+rab*cosd(th3(t,1))*w3;
sol(t)=vpasolve([eq3 eq4],[w3 w4]);%PROBLEM IS HERE
end
w3=zeros(27,1);
w4=zeros(27,1);
for a=1:27
w3(a,1)=sol(a).w3;
w4(a,1)=sol(a).w4;
end
I get correct results for theta2 theta3 and theta4 but when I try to solve another equation by using the same way I am having this error;
Subscripted assignment between dissimilar structures.
Error in Untitled2 (line 26)
sol(t)=vpasolve([eq3 eq4],[w3 w4]);
I did a search online but I couldn't understand answers, I think there's something different. Could you help me please?
Kubilay AKPINAR
2020 年 3 月 26 日
I detected my mistake, I must use different names for this piece of code;
sol(i)=vpasolve([eq1 eq4],[x2 x3]);
sol(t)=vpasolve([eq3 eq4],[w3 w4]);
there are two "sol" matrices, that's the problem. I changed the second one as "solu" and the error disappeared. Thanks anyway.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Programming についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
