2013-10-16 63 views
0

我對此表示歉意,我相信這是一個簡單的任務,但我不知道該怎麼做。如何繪製帶定製座標軸的圖形

假設我有一個公式y = (exp(-x) + x^2)/sqrt(pi(x),我想將它繪製爲yx^2

這是怎麼回事?

+1

您的公式有錯誤,遺漏括號,缺少'.'s的元素方面的操作,也'PI(X)'是極不可能的,你是什麼意思。請修復這些。 – Dan

回答

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 

第二個方法讓你像一個陰謀 enter image description here

編輯:

其實我覺得你問的這個:

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) 

所以我在這裏繪製一個簡單的二次曲線,但如果我將它繪製成平方它應該變成線性的。但是我仍然想讀出y = x^2而不是y = x的圖,我只是想讓它看起來像y = x。因此,如果我讀取該圖上x值爲4的y值,我將得到16,這仍然是相同的正確的原始y值。

enter image description here

+0

如果我正確理解OP的問題,它不應該是'y =(exp(-X + X.^2)./ sqrt(pi * X);'''''''是'x'的函數,而不是'x^2'? – am304

+0

Y仍然是'x'這種方式的函數,您只需要爲'x。^ 2'繪製點,以便爲曲線上的每個x值獲得正確的y值。 – Dan

+0

但關鍵是在你的代碼中,你計算'y'是'x^2'而不是'x'的函數,我認爲這是不正確的。你可以計算'y'爲'x'的函數,但將其繪製爲「x^2」的函數。 – am304

0

這裏是我的答案是:它類似於丹的一個,但根本的不同。你可以計算的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 

enter image description here

+0

我真的認爲這是不正確的。你有錯誤的y值。對於x = 4,y也應該在4左右,你的標繪爲1.我真的認爲OP只是想把tickmarks改成非線性的。這扭曲了實際值,不再是y = f(x)。我的仍然是。 – Dan

+0

我不同意。 OP希望'y = f(x^2)',而不是'y = f(x)'。當'x^2'是4時,'x'是2,'y'是1.65左右。 – am304

+0

OP希望**對**繪製**而不是基於'x^2'來計算它。這就像將軸改爲對數刻度而不是線性。這些值不應該改變。只是情節。 – Dan