0

I apologize for asking this, I believe this is a simple task, but I don't know how to do it.

Suppose I have a formula y = (exp(-x) + x^2)/sqrt(pi(x) and I want to plot it as y versus x^2.

How does one do this?

1
  • 1
    Your formula has errors, missing brackets, missing .s for element-wise operations and also pi(x) is highly unlikely what you mean. Please fix these. Commented Oct 16, 2013 at 8:07

2 Answers 2

1

Like this:

 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.

At this point this is no different to having just plotted it normally. What you want is to make the tickmarks go up in values of X.^2. This does not change the y-values nor distort the function, it just changes what it looks like visually. Similar to plotting against a log scale:

set(gca, 'XTick', X.^2)  %//Set the tickmarks to be squared

The second method gives you a plot like enter image description here

edit:

Actually I think you were asking for this:

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)

So here I'm plotting a simple quadratic, but if I plot it against a squared x it should become linear. However I still want to read off the graph that y=x^2, not y=x, I just want it to look like y=x. So if I read the y value for the x value of 4 on that graph i will get 16 which is still the same correct original y value.

enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

If I understand the OP's question correctly, shouldn't it be y = (exp(-X + X.^2)./sqrt(pi*X); i.e. y is a function of x and not x^2?
Y is still a function of x this way, you're just plotting points for x.^2 so that you get the correct y value for each x value on the plot.
but the point is that in your code, you are calculating y as a function of x^2, not x, which I think is incorrect. You can calculate y as a function of x, but plot it as a function of x^2.
@am304 I don't agree. Try this: X = 0:0.1:5; x = X.^2; y = (exp(-x) + x.^2)./sqrt(pi*x); Y = (exp(-X) + X.^2)./sqrt(pi*X); plot(X,Y); plot(x,y); figure(2); set(gca, 'XTick', x); You can see that if you zoom in on the second plot they are the same. It's still a function of x just calculated at non-linear intervals.
My point is if you do plot(X,y,X,Y), the two curves aren't the same. (X,Y) is the correct one, (X,y) is wrong.
|
0

Here's my answer: it is similar to Dan's one, but fundamentally different. You can calculate the values of y as a function of x, but plot them as a function of x^2, which is what the OP was asking, if my understanding is correct:

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

As Dan mentioned, you can always change the tickmarks:

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

6 Comments

I really think this is incorrect. You have the wrong y values. For x = 4, y should also be around 4, yours is plotting at 1. I really think the OP just wanted to change the tickmarks to be non-linear. This distorts the actual values and is NOT y = f(x) anymore. Mine still is.
I disagree. The OP wants y=f(x^2), not y=f(x). When x^2 is 4, then x is 2 and y is around 1.65.
The OP wants to plot against X^2 not calculate it based on x^2. It's just like changing an axis to a log scale instead of linear. The values shouldn't change. Just the plot.
Yes, that's precisely why I calculate y as a function of x, but plot it as a function of x^2.
OK, so I agree your plot image is correct! But your labels are wrong. The values can't change. f(x) must still be y, not some new y' but the plot must distort to look like yours does and not like mine. But the distance between equal values on the x axis should no longer be the same. I suggest just adding this to the end of your code set(gca, 'XTickLabel', 0:0.5:5) and then I will say that this is the correct answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.