簡體   English   中英

如何在Matlab中繪制二次函數(具有不同比例的軸)

[英]How to plot a quadratic function in Matlab (with differently scaled axes)

我正在嘗試創建一個具有2個不同比例軸的根函數的圖,因此假設x軸以0.1的步長從0到1.2變化,y軸以0.2的步長從0到1.4變化(一個函數是2不同比例的軸)。 我認為縮放比例正確,如果有更好的編程方法,請更正我。

這是我的代碼:

x = linspace(0,1.2);
y = 0.5 + (0.9 * (x.^2 - 0.0432)).^(1/2);

% here I need the negative part as well: 0.5 - [...] as follows:
% y2 = 0.5 - (0.9 * (x.^2 - 0.0432)).^(1/2); 
% How can I create this function and plot it?

plot(x,y)
axis([0 1.2 0 1.4])
set(gca,'xTick',0:0.1:1.2)
set(gca,'yTick',0:0.2:1.4)

grid on

我有函數的上部,但沒有下部函數(負數,請參見上面的代碼注釋)。 如何創建? 還是如果不可能,如何從定義不同的“子圖”中創建圖? 該域需要以某種方式限制為x> = 0.206。

你近了! 我將執行以下操作。 請參閱以下代碼中的注釋:

n = 1000;                % number of points. More points, smoother 
                         % looking piecewise linear approx. of curve

x0 = sqrt(.0432)+eps;    % Choose smallest xvalue to be at or epsilon to the right
                         % of the apex of the parabola

x = linspace(x0, 1.2, n)';   %'  transpose so x is a column vector (more convenient)
y_pos = 0.5 + (0.9 * (x.^2 - 0.0432)).^(1/2); % positive branch of parabola
y_neg = 0.5 - (0.9 * (x.^2 - 0.0432)).^(1/2); % negative branch of parabola

plot(x,[y_pos, y_neg],'blue')  % we´re graphing two series but use 'blue'
                               % for both so it looks like one series!
axis([0 1.2 0 1.4])
set(gca,'xTick',0:0.1:1.2)
set(gca,'yTick',0:0.2:1.4)

grid on

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM