現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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 件のコメント
Ian
2011 年 2 月 7 日
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
Walter Roberson
2011 年 2 月 7 日
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);
Ian
2011 年 2 月 7 日
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.
Walter Roberson
2011 年 2 月 7 日
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.
Walter Roberson
2011 年 2 月 7 日
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);
Ian
2011 年 2 月 7 日
Hmmm...It doesn't seem to be substituting in the value for f still. No errors though :).
Walter Roberson
2011 年 2 月 7 日
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
Walter Roberson
2011 年 2 月 7 日
You could try subs('phi1-f=0','f',f)
but in theory that should not be needed.
Walter Roberson
2011 年 2 月 7 日
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.)
Ian
2011 年 2 月 7 日
Awesome, yes the second works!! Thanks!! Only problem, it displays in 16bit fractions... I can get that working though.
Walter Roberson
2011 年 2 月 7 日
double() the result to get IEEE double precision floating point.
Ian
2011 年 2 月 7 日
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!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)
