Optimizing this script without using symbolic toolbox

16 ビュー (過去 30 日間)
Kasper
Kasper 2011 年 4 月 28 日
Hi guys, this is the script which takes up all the time
function [t_sol,xi,yi] = solveIntersection(px,py,rx,ry,r)
syms t
x = px + t*rx;
y = py + t*ry;
c = x^2+y^2-r^2;
t_sol = max(double(solve(c)));
xi = double(subs(x,'t',t_sol));
yi = double(subs(y,'t',t_sol));
This script calculates the intersection of a circle and a given vektor. How do i calculate the intersection without using the symbolic toolbox? Or just how can I optimize this function?
The vector starts in (px,py) and has a direction (rx,ry). r is the radius of the circle.

採用された回答

Andrew Newell
Andrew Newell 2011 年 4 月 28 日
If you run this code
syms t px py rx ry r
x = px + t*rx;
y = py + t*ry;
c = expand(x^2+y^2-r^2);
c = collect(c,t)
solve(c,t)
you'll get explicit equations for the two solutions for t. These can be put in the function:
function [t,xi,yi] = solveIntersectionNum(px,py,rx,ry,r)
t = [-(px*rx + py*ry + (- px^2*ry^2 + 2*px*py*rx*ry - py^2*rx^2 + r^2*rx^2 + r^2*ry^2)^(1/2))/(rx^2 + ry^2)
-(px*rx + py*ry - (- px^2*ry^2 + 2*px*py*rx*ry - py^2*rx^2 + r^2*rx^2 + r^2*ry^2)^(1/2))/(rx^2 + ry^2)];
xi = px + t*rx;
yi = py + t*ry;
It's about 100 times faster.
  5 件のコメント
Kasper
Kasper 2011 年 4 月 29 日
Yes what if I want to analyze an ellipse instead. But I could make 2 different functions. One for ellipsis and one for circles.
Andrew Newell
Andrew Newell 2011 年 4 月 29 日
A circle is a special case of an ellipse ...

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConversion Between Symbolic and Numeric についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by