이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Hello all,
I'm using the solve function to solve a system of non-linear equations and I'm having a little trouble. I'm trying to input a function f into one of the equations, and then solve for the 3 variables. The problem is that it treats f as a symbolic variable. The order is also messed up, but I think I just need to specifically call the solutions for each variable.
Here is the code:
function kinematicanal
clc;
syms x1 y1 phi1;
for k=1:1:11
t=(k-1)/10;
f=sin(10*t);
[x1sol(k),y1sol(k),phi1sol(k)]=solve('x1-sin(phi1)','y1+cos(phi1)','phi1-f',x1,y1,phi1);
q=[x1sol;y1sol;phi1sol];
end
q
Can anyone help? Thanks, Ian
Edit: That should be a bit easier to read the code
채택된 답변
Walter Roberson
2011년 2월 7일
[x1sol(k), y1sol(k), phi1sol(k)] = solve(x1-sin(phi1), y1+cos(phi1), phi1-f, x1, y1, phi1);
Question: why do you bother constructing q within the loop? You overwrite it each time through the loop, and you overwrite it with the complete vectors, so the end result would be the same as constructing it once after the loop.
Question 2: why are you not pre-allocating space for x1sol, y1sol, phi1sol?
Question 3: why do you not just do the solve() once outside of any loop, with k symbolic? You would get out a single x1sol, y1sol, phi1sol that was parameterized in terms of k. matlabFunction() the result if you need to, and feed it 1:11 and you will get all the answers at once.
Question 4: if t = (k-1)/10 and f=sin(10*t) and you do not use t for anything else, then why not skip t and go directly for f = sin(k-1) ?
Question 5: are you sure you want to be taking the sin() of an integer number of radians???
댓글 수: 12
Sorry, those questions do not help. I'm answering a specific problem and I need to write the code in a specific format. I would like to know what t and f are different times without having to back calculate. And yes, I want the sine of an integer using radians.
Also my computer computes this in about 20 milliseconds, I'm not really worried about efficiency at this point.
Could someone help me find out how to plug in variable f into the solve function, I don't really need any other feedback right now.
I appreciate the help,
Ian
The line of code I gave *does* plug f into the solve function.
If it is strictly necessary (due to external constraints) to pass quoted strings instead of symbolic expressions, then:
[x1sol(k), y1sol(k), phi1sol(k)] = solve('x1-sin(phi1)', 'y1+cos(phi1)', subs('phi1-f'),x1, y1, phi1);
Oh...Sorry, I thought that was a quote from the original post. I had tried symbolic expressions first, I keep getting an error similar to this:
"
Warning: 2 equations in 4 variables. New variables might be introduced.
Warning: Could not extract individual solutions. Returning a MuPAD set object.
> In solve>assignOutputs at 104
In solve at 87
In kinematicanal at 12
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Error in ==> kinematicanal at 12
[x1sol(k), y1sol(k), phi1sol(k)] = solve(x1-sin(phi1), y1+cos(phi1), phi1-f, x1, y1, phi1);"
I tried using subs and get the same thing. Thanks very much for your help, I thought you only criticized my code in the first post and didn't answer the question. I appreciate the feedback.
Could I ask you to go back to the original question and edit it so that each line of code is formatted on to a line of its own? It might just mean adding two spaces to the beginning of each code line.
I want to be sure that I am working with the same code you are working with; the forum has wrapped some of the lines automatically in ways that might be deceiving me.
Ah, the first iteration through, f is sin(0) which will be 0, so phi1 - f will be phi1, so that part is being interpreted as a symbolic variable name rather than something to be solved for 0.
[x1sol(k), y1sol(k), phi1sol(k)] = solve('x1-sin(phi1)=0', 'y1+cos(phi1)=0', subs('phi1-f=0'), x1, y1, phi1);
Hmmm...It doesn't seem to be substituting in the value for f still. No errors though :).
Thanks for the code cleanup. I can now be sure that the problem was what I outlined above about phi1 - 0 being treated as phi1
You could try subs('phi1-f=0','f',f)
but in theory that should not be needed.
For debugging, before the solve line you might want to display the result of
subs('phi1-f=0')
and
subs('phi1-f=0','f',f)
to see which subs() is working (if either.)
Awesome, yes the second works!! Thanks!! Only problem, it displays in 16bit fractions... I can get that working though.
double() the result to get IEEE double precision floating point.
Got it, thanks a lot for your help!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Functional Programming에 대해 자세히 알아보기
태그
참고 항목
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)
