我很抱歉提出这个问题,我相信这是一项简单的任务,但我不知道该怎么做。
假设我有一个公式y = (exp(-x) + x^2)/sqrt(pi(x)
,我想将其绘制为y
vs x^2
。
如何做到这一点?
我很抱歉提出这个问题,我相信这是一项简单的任务,但我不知道该怎么做。
假设我有一个公式y = (exp(-x) + x^2)/sqrt(pi(x)
,我想将其绘制为y
vs x^2
。
如何做到这一点?
像这样:
X = 0:0.1:5; %// Get the x values
x = X.^2; %// Square them
%// Your formula had errors, I fixed them but I could have misinterpreted here, please check
y = (exp(-x) + x.^2)./sqrt(pi*x); %// Calculate y at intervals based on the squared x. This is still y = f(x), I'm just calculating it at the points at which I want to plot it.
plot(x,y) %//Plot against the square X.
在这一点上,这与刚刚正常绘制它没有什么不同。您想要的是使刻度线的值上升X.^2
。这不会改变 y 值,也不会扭曲函数,它只会改变它在视觉上的样子。类似于根据对数比例绘制:
set(gca, 'XTick', X.^2) %//Set the tickmarks to be squared
第二种方法给你一个情节
编辑:
其实我想你是在问这个:
x = 0:0.1:5;
y = x.^2; %// Put your function in here, I'm using a simple quadratic for illustrative purposes.
plot(x.^2,y) %//Plot against the square X. Now your y values a f(x^2) which is wrong, but we'll fix that later
set(gca, 'XTick', (0:0.5:5).^2) %//Set the tickmarks to be a nonlinear intervals
set(gca, 'XTickLabel', 0:0.5:5) %//Cahnge the labels to be the original x values, now accroding to the plot y = f(x) again but has the shape of f(x^2)
所以在这里我绘制了一个简单的二次曲线,但是如果我将它与平方 x 绘制,它应该变成线性的。但是我仍然想读取 y=x^2,而不是 y=x 的图表,我只是希望它看起来像 y=x。因此,如果我在该图上读取 4 的 x 值的 y 值,我将得到 16,这仍然是相同的正确原始 y 值。
这是我的答案:它与丹的相似,但根本不同。您可以将 的值计算y
为 的函数x
,但将它们绘制为 的函数x^2
,如果我的理解正确,这就是 OP 所要求的:
x = 0:0.1:5; %// Get the x values
x_squared = x.^2; %// Square them
%// Your formula had errors, I fixed them but I could have misinterpreted here, please check
y = (exp(-x) + x.^2)./sqrt(pi*x); %// Calculate y based on x, not the square of x
plot(x_squared,y) %//Plot against the square of x
正如 Dan 提到的,您可以随时更改刻度线:
x_ticks = (0:0.5:5).^2; % coarser vector to avoid excessive number of ticks
set(gca, 'XTick', x_ticks) %//Set the tickmarks to be squared