有趣的圓錐曲線

function plot_conic_2D()
??fig = figure('Position',[100 100 800 600]);
??ax = axes('Parent',fig,'Position',[0.2 0.2 0.6 0.7]);
??% 初值
??m=3;
??n=2;
??rho=0.5;
??[X,Y]=meshgrid(-5:0.2:5);
??Z=(1/(1 - rho^2))(X.^2/m^2+Y.^2/n^2-2*rho.*X.*Y./(m*n))-1;
??[~,hc] = contour(ax,X,Y,Z,[0,0],'LineWidth',2); % 繪制等高線
??colormap(gca,'summer');
??% 繪制控制面板
??uicontrol('style','slider',...
???????'units','normalized',...
???????'position',[0.05 0.85 0.1 0.05],...
???????'min',1,'max',10,'value',m,...
???????'callback',{@update_plot,ax},'tag','slider_m');
??uicontrol('style','text',...
???????'units','normalized',...
???????'position',[0.05 0.9 0.1 0.05],...
???????'string','Parameter m','tag','text_m');
??uicontrol('style','edit',...
???????'units','normalized',...
???????'position',[0.2 0.85 0.05 0.05],...
???????'string',num2str(m),...
???????'callback',{@update_parameter,'m'},'tag','edit_m');
??uicontrol('style','text',...
???????'units','normalized',...
???????'position',[0.25 0.85 0.1 0.05],...
???????'string','(range: [1,10])','tag','text_m_range');
??uicontrol('style','slider',...
???????'units','normalized',...
???????'position',[0.05 0.75 0.1 0.05],...
???????'min',0.5,'max',5,'value',n,...
???????'callback',{@update_plot,ax},'tag','slider_n');
??uicontrol('style','text',...
???????'units','normalized',...
???????'position',[0.05 0.8 0.1 0.05],...
???????'string','Parameter n','tag','text_n');
??uicontrol('style','edit',...
???????'units','normalized',...
???????'position',[0.2 0.75 0.05 0.05],...
???????'string',num2str(n),...
???????'callback',{@update_parameter,'n'},'tag','edit_n');
??uicontrol('style','text',...
???????'units','normalized',...
???????'position',[0.25 0.75 0.1 0.05],...
???????'string','(range: [0.5,5])','tag','text_n_range');
??uicontrol('style','slider',...
???????'units','normalized',...
???????'position',[0.05 0.65 0.1 0.05],...
???????'min',-1,'max',1,'value',rho,...
???????'callback',{@update_plot,ax},'tag','slider_rho');
??uicontrol('style','text',...
???????'units','normalized',...
???????'position',[0.05 0.7 0.1 0.05],...
???????'string','Parameter rho','tag','text_rho');
??uicontrol('style','edit',...
???????'units','normalized',...
???????'position',[0.2 0.65 0.05 0.05],...
???????'string',num2str(rho),...
???????'callback',{@update_parameter,'rho'},'tag','edit_rho');
??uicontrol('style','text',...
???????'units','normalized',...
???????'position',[0.25 0.65 0.1 0.05],...
???????'string','(range: [-1,1])','tag','text_rho_range');
end
function update_plot(hObject,eventdata,ax)
??% 獲取參數(shù)
??m = get(findobj('tag','slider_m'),'Value');
??n = get(findobj('tag','slider_n'),'Value');
??rho = get(findobj('tag','slider_rho'),'Value');
??% 更新等高線
??[X,Y]=meshgrid(-5:0.2:5);
??Z=(X.^2/m^2+Y.^2/n^2-2*rho.*X.*Y./(m*n))-1;
??set(gca,'ZLim',[-5 5])
??[~,hc] = contour(ax,X,Y,Z,[0,0],'LineWidth',2);
??colormap(gca,'summer');
??% 移除舊的等高線標(biāo)簽
??delete(findall(gcf,'type','text'));
??% 添加新的等高線標(biāo)簽
??[~,h] = contourcs(X,Y,Z,[0,0]);
??for i=1:length(h)
????x = mean(h(i).XData);
????y = mean(h(i).YData);
????label_str = sprintf('m=%.1f,n=%.1f,\\rho=%.1f', m, n, rho); % 在標(biāo)簽中添加參數(shù)值
????text(x,y,label_str,'Color','blue','FontWeight','bold','HorizontalAlignment','center');
??end
??% 更新坐標(biāo)軸
??axis(ax,[-5 5 -5 5]);
end
function update_parameter(hObject,eventdata,param_name)
??% 獲取當(dāng)前值
??new_value = str2double(get(hObject,'String'));
??% 判斷值是否在指定范圍內(nèi),如果不是,將其限制到范圍邊界上
??switch param_name
????case 'm'
??????if new_value < 1
????????new_value = 1;
??????elseif new_value > 10
????????new_value = 10;
??????end
??????set(findobj('tag','slider_m'),'Value',new_value);
????case 'n'
??????if new_value < 0.5
????????new_value = 0.5;
??????elseif new_value > 5
????????new_value = 5;
??????end
??????set(findobj('tag','slider_n'),'Value',new_value);
????case 'rho'
??????if new_value < -1
????????new_value = -1;
??????elseif new_value > 1
????????new_value = 1;
??????end
??????set(findobj('tag','slider_rho'),'Value',new_value);
??end
??% 將文本框中的值更新為新值
??set(hObject,'String',num2str(new_value));
end