Mary Star
Mary Star

Reputation: 375

Adjusting the y-axis range in Matlab

I need some help in Matlab. I have to implement the Newton Method and plot the function f and some approximations of the method. I used the following code:

plot(x,f(x))
hold on
for j=1:4
    x_1=x_0-f(x_0)/F(x_0);
    l=@(x) (f(x_0)/(x_0-x_1))*(x-x_1);
    plot(x_0,f(x_0),x,l(x),x_1,0)
    x_0=x_1;   
end

Is this correct? When I plot it, the range of y-axis is [-2000, 30000]. How can I change it? How can I make it smaller so that I can see the result better?

Upvotes: 0

Views: 2878

Answers (3)

Floris
Floris

Reputation: 46365

I would keep track of the smallest and largest "interesting" values of x by adding

xMin = min(x0, xMin);
xMax = max(x0, xMax);

In you loop (after appropriately initializing xMin and xMax) - then changing the x axis with:

xlim([xMin xMax]):

As was pointed out, if you leave the other axis alone it will scale by itself. You could change things by increasing or decreasing the range, for example

delta = xMax - xMin;
xlim([xMin - 0.2*delta xMax + 0.2*delta)];

Upvotes: 1

chappjc
chappjc

Reputation: 30579

To just change the y-axis, have a look at ylim:

ylim([ymin ymax])

Similarly, there is an xlim command. By default, these will be in auto mode (i.e. ylim('auto') and xlim('auto')) so that when you change the range of the x-axis, the y-axis range will automatically change to an appropriate range for the visible data.

Upvotes: 2

David
David

Reputation: 8459

Using

axis([xmin xmax ymin ymax])

you can set the axis limits to whatever you want.

Upvotes: 2

Related Questions