You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Two for loops taking very long to get the complete solutions
1 view (last 30 days)
Show older comments
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 Comments
Stephen23
on 6 Nov 2018
"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.
Accepted Answer
madhan ravi
on 6 Nov 2018
Edited: madhan ravi
on 6 Nov 2018
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 Comments
AtoZ
on 6 Nov 2018
Edited: AtoZ
on 6 Nov 2018
@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
on 6 Nov 2018
`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
on 6 Nov 2018
Solve your equation once outside the loop and only substitute k and x values into the solution inside the loop.
Stephen23
on 6 Nov 2018
Edited: Stephen23
on 6 Nov 2018
"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
on 6 Nov 2018
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
on 6 Nov 2018
@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
on 6 Nov 2018
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
on 6 Nov 2018
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
on 6 Nov 2018
Edited: Stephen23
on 6 Nov 2018
"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
on 6 Nov 2018
Edited: Bruno Luong
on 6 Nov 2018
'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
on 6 Nov 2018
Edited: Bruno Luong
on 6 Nov 2018
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');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/194516/image.png)
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)