0

Trying to plot a function 2 times(once with x1,x2 = -2,2 , second with x1,x2 = -1,2) with the code below;however, only the second plot shows:

% We set the domain to [x1,x2];
x1 = [-2,-1]
x2 = [2,2]

% We plot the graphs
for i = 1:length(x1)
    [domain,range,plot] = vector(x1(i),x2(i))
end

function [x,y,fplot] = vector(a,b)
    x = [linspace(a,b)]
    f = @(x) x.^2
    y = f(x)
    fplot = plot(x,y)
end
3
  • 1
    unless you make a new figure, or tell MATLAB to hold on, it will replace the previous plot Commented Feb 9, 2021 at 14:59
  • Why do create a function and directly call it instead of simply y = x.^2? Commented Feb 9, 2021 at 15:02
  • @AnderBiguri, thanks, adding figure in the loop works. @ThomasSablik, didn't know that, it clears some space, thank you Commented Feb 10, 2021 at 12:50

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.