如何在Matlab中绘制二次函数(使用不同比例的轴)

时间:2015-12-09 14:57:24

标签: matlab plot quadratic

我试图创建一个带有2个不同缩放轴的根函数图,所以让我们说x轴从0到1.2,步长为0.1,y轴从0到1.4,步长为0 0.2(一个函数,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。

1 个答案:

答案 0 :(得分:3)

你关闭了!我会做以下事情。请参阅以下代码中的评论:

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