solve the intersection of the line and surface

3 ビュー (過去 30 日間)
xingjian yang
xingjian yang 2018 年 5 月 19 日
コメント済み: xingjian yang 2018 年 5 月 20 日
I try to solve the intersection of a line and a surface by solving a system of nonlinear equations, while it seems to run into some problem of the global variables and function handle...
global x y z t
line_p=[3,2,1]; % point on the line
line_d=[1,2,3]; % line direction vector
surface=@(x,y,z)x+y^2+z^3-10;
p=intersection(line_p,line_d,surface);
function [p]=intersection(line_p,line_d,surface)
eq1=x-(line_p(1)+line_d(1)*t);
eq2=y-(line_p(2)+line_d(2)*t);
eq3=z-(line_p(3)+line_d(3)*t);
eq4=@(x,y,z)surface;
sol = solve(eq1,eq2,eq3,eq4,x,y,z,t);
p=sol(3,1);
end

採用された回答

Rik
Rik 2018 年 5 月 19 日
You need to declare globals in each function you use them to enable the functionality.
You should in general avoid global variables, because they are very difficult to debug. If you really have to use them, use very long, very descriptive names that you will never use again. If that name is too cumbersome to use, just copy the variable, see the example below.
function myfunction
global myfunction_global_variable__frequency_of_red_cars_from_Montana
cars=myfunction_global_variable__frequency_of_red_cars_from_Montana;
plot(cars)
end
  1 件のコメント
xingjian yang
xingjian yang 2018 年 5 月 20 日
Thank you! It's clearly explained

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by