Search code examples
matlabgraphmatlab-figurematlab-guide

How to zoom in or magnify programmatically in MATLAB plot graph?


I'm working on a script, where I need to show graph of Fixed Point Iteration and want to zoom in where lines are plotting. Graph is done, it is plotting lines on my graph, problem is that I want to see the values which are produced from function from f(x) and g(x). So I "zoom in" from the figure tools and see which values are produced in the graph right now I am doing it manually in the figure, is there any way to automatically zoom in by just giving the x, y axis? so it means when lines are plotting figure will be automatically zoomed like an animation.

clc;
clear all;
clf;
format short g

syms x;
f = @(x) x-cos(x);
g = @(x) cos(x);
dg = matlabFunction(diff(g(x),x));

figure(1)
z = -3:.001:3;
plot(z,z,'-k',z,g(z),'-k',z,0*z,'-r',0*z,g(z),'-r')
hold on;

x = 1.0;
tol =1.0e-15;
px = x;
x = g(x);
line([px,px],[px,x],'color','blue');
line([px,x,],[x,x],'color','blue');
i = 1;
while(abs(px-x)>tol)
   px = x;
   x = g(x);
   line([px,px],[px,x],'color','blue');
    line([px,x,],[x,x],'color','blue');
   i = i+1;
   data = [i  x g(x) f(x)]
   drawnow
end

This is what I want in my graph lines. Link

I tested "zoom" function but it is not helping as per my requirement. Also I tried this one but I can't understand there code.


Solution

  • xlim will allow you to set the range of x-values that are shown. Link: xlim

    ylim will allow you to set the range of y-values that are shown. Link: ylim

    Together, these should allow you to 'zoom' to different parts of your plot. For example, after your first plot command you could include:

    plot(z,z,'-k',z,g(z),'-k',z,0*z,'-r',0*z,g(z),'-r') % Already in your code
    xlim([0 1.5]);
    ylim([0 1.5]);
    

    You could also include these commands in your for loop so that it gradually zooms in.