我为此问道歉,我相信这是一项简单的任务,但我不知道该怎么做。
假设我有一个公式y = (exp(-x) + x^2)/sqrt(pi(x)
,我想将其绘制为y
与x^2
。
如何做到这一点?
答案 0 :(得分:1)
像这样:
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。因此,如果我在该图上读取x值为4的y值,我将得到16,这仍然是正确的原始y值。
答案 1 :(得分:0)
这是我的答案:它与Dan的相似,但根本不同。您可以将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
正如丹所提到的,您可以随时更改标记:
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