دوشنبه, ۲ دی ۱۳۹۸، ۱۲:۳۶ ب.ظ
محاسبه زوایه بین دو خط در متلب (calculation angle between two lines in Matlab)
طبق مثالی که اوردم ، سه نقطه به عنوان ورودی در نظر گرفته شده، که نقطه اول C به عنوان مبداء و نقطه اتصال خط a و خط b ، نقطه B ( انتهای خط a) نقطه A، انتهای خط b. دقت کنین که زاویهها با حروف بزرگ و خطوط روبروی هر زاوایه حرف کوچک آن زوایه میباشد.
شما با تغییر مقادیر نقاط میتوانید زاویههای مرتبط با آنها را مشاهده کنید..
کد متلب این مثال:
%% clear commands
delete(allchild(0));% delete all figures and graphical objects
clear;% Remove items from workspace, freeing up system memory
clc;% clear the Console
%% path commands
file=matlab.desktop.editor.getActive;% get current script address
try % if dont occur any error this block will run
fileDetail=dir(file.Filename);% get current script details
folderName=fileDetail.folder;% get current script folder name
catch% else if an error occurs then
index=strfind( file.Filename,'\');% find index of back space(\) in path text
folderName=file.Filename(1:index(end)-1);% select all path text from 1 to last Occurrence of \ as script folder name
end
paths=genpath(folderName);% make current path and all sub paths
addpath(paths);% add all paths in known paths of matlab
cd(folderName);% go to path that current script runned from that
A_x=5;
A_y=12;
B_x=A_x/2;
B_y=A_y*2;
C_x=0/3;
C_y=0;
c=sqrt( ((B_x-A_x)^2) +( (B_y-A_y)^2));
b=sqrt( ((A_x-C_x)^2) +( (A_y-C_y)^2));
a=sqrt( ((B_x-C_x)^2) +( (B_y-C_y)^2));
tSlop=(a^2 +b^2 -c^2)/(2*a*b);
alpha_rad= (acos(tSlop));
alpha_deg=rad2deg(acos(tSlop));
figure;
plot([ C_x,A_x],[C_y,A_y],'r-');hold on
plot([ C_x,B_x],[C_y,B_y],'r-');hold on
plot([ A_x,B_x],[A_y,B_y],'b--');hold on
text(A_x,A_y,'A','Horizontalalignment', 'right');
text(B_x,B_y,'B','Horizontalalignment', 'left');
text(C_x,C_y,'C','Horizontalalignment', 'center');
text((A_x+C_x)/2 ,(A_y+C_y)/2,'b','Horizontalalignment', 'right');
text((B_x+C_x)/2 ,(B_y+C_y)/2,'a','Horizontalalignment', 'left');
text((A_x+B_x)/2 ,(A_y+B_y)/2,'c','Horizontalalignment', 'center');
text(C_x+2 ,C_y+2,[ '\alpha= ' num2str(alpha_deg) ' deg ' ' = ' num2str(alpha_rad) ' rad '],'Horizontalalignment', 'center');
axis('equal')
خروجی نمونه( output example )

۹۸/۱۰/۰۲