이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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 (한국어)