現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Two for loops taking very long to get the complete solutions
1 回表示 (過去 30 日間)
古いコメントを表示
AtoZ
2018 年 11 月 6 日
The two for loops in the code take very long to complete
clear all
syms a
for k=0:0.1:pi
for x=0:0.1:pi
f = sin(k+a) - x*cos(x);
y=vpasolve(f,a,[0 pi])
end
end
How to fix this?
4 件のコメント
Stephen23
2018 年 11 月 6 日
"How to fix this?"
Do not use vpasolve in a loop.
Seriously, symbolic maths is slow. If you want fast code, find the solution numerically.
採用された回答
madhan ravi
2018 年 11 月 6 日
編集済み: madhan ravi
2018 年 11 月 6 日
EDITED
syms k a x
f = sin(k+a) - x*cos(x);
solution=solve(f,a)
x=0:0.1:pi;
k=x;
solutions=vpa(subs(solution),3)
19 件のコメント
AtoZ
2018 年 11 月 6 日
編集済み: AtoZ
2018 年 11 月 6 日
@madhan thanks. Could you please also say something about `cell(1)` and ctr=1; what are these? Secondly,
if isempty(y)==0
break
Is it excluding the regions with no solutions? Actually there should be some regions where there's no solution, so I want to also know those regions explicitly. and the output format is very long, I tried to put
format short
at different places in your code but it didn't work.
madhan ravi
2018 年 11 月 6 日
`cell(1)` and ctr=1;
its called as pre-allocation for increasing speed and efficiency of the loop car is just a basic counter, see edited answer
Torsten
2018 年 11 月 6 日
Solve your equation once outside the loop and only substitute k and x values into the solution inside the loop.
Stephen23
2018 年 11 月 6 日
編集済み: Stephen23
2018 年 11 月 6 日
"its called as pre-allocation for increasing speed and efficiency ..."
Nope. Note that cell(1) only creates a 1x1 cell array, so does not preallocate the cell array to its final size, or anything close to it.
Also note that isempty(y)==0 will always be true, so it serves no useful purpose.
madhan ravi
2018 年 11 月 6 日
syms k a x
f = sin(k+a) - x*cos(x);
solution=solve(f,a)
x=0:0.1:pi;
k=x;
solutions=vpa(subs(solution),3)
AtoZ
2018 年 11 月 6 日
@madhan Thanks. This last one was quicker. What does 3 in
solutions=vpa(subs(solution),3)
mean? Sorry but i am very new to Matlab :-)
Torsten
2018 年 11 月 6 日
I don't understand why you don't take the code I suggested under
https://de.mathworks.com/matlabcentral/answers/428022-numerical-solution-of-a-trigonometric-equation
It's efficient and takes into account the constraint on a.
AtoZ
2018 年 11 月 6 日
I actually didn't understand why did we need to use all those conditions and why not just a straightforward solution like this one. I am actually trying to get the full solutions in the form of a table(x,k). I will try to do it if I can. Or if you know how, you could post there and I will accept. Thanks
Stephen23
2018 年 11 月 6 日
編集済み: Stephen23
2018 年 11 月 6 日
"I actually didn't understand why did we need to use all those conditions and why not just a straightforward solution like this one."
Torsten's code is a straightforward solution. It is very simple!
You are failing to take into account the complexity of symbolic maths. Just because it is hidden inside two commands (e.g. syms and vpasolve) does not mean that symbolic maths is simple or fast. You are comparing a few simple lines of numeric code with a huge symbolic black-box, which is about as apples and oranges as you can get. Just because you are not looking at the black box does not mean that you can disregard its effects.
Bruno Luong
2018 年 11 月 6 日
編集済み: Bruno Luong
2018 年 11 月 6 日
'I actually didn't understand why did we need to use all those conditions and why not just a straightforward solution like this one.' Thanks
The apparent complication because that fact that you deliberately restrict the solution a in [0,pi] as show in pour VPA code.
But Torsten code is the most efficient instead of using those horrible vpasove for something that can be computed from formula and few tests.
Torsten solution is fatest, simplest and mots reliable.
Bruno Luong
2018 年 11 月 6 日
編集済み: Bruno Luong
2018 年 11 月 6 日
If you need a vectorized Torsen code, here it is
x=0:0.1:pi;
k=(0:0.1:pi)';
b=asin(x.*cos(x));
b(imag(b)~=0)=NaN;
a1 = mod(b-k,2*pi);
a2 = mod(pi-b-k,2*pi);
a = a1;
outside = a1>pi & a2<=pi;
a(outside) = a2(outside);
a(a>pi) =NaN;
close all
surf(x,k,a);
xlabel('x');
ylabel('k');
zlabel('a');
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
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)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)