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!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 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 (한국어)