1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Graphs in matlab

  1. Nov 18, 2009 #1
    1. The problem statement, all variables and given/known data

    I have two functions in matlab that return y1 and y2 as the solutions and both functions depend on x.
    I plotted them on the same graph and there is a gap between the graphs as expected (i.e they are not on top of each other).
    Now, what i want to do is find the difference between these graphs.

    2. Relevant equations



    3. The attempt at a solution

    If the function for y1 is called f1 and that of y2 is called f2. i get an error if i do:
    a=f1-f2

    so, how can i find the difference?

    thank you
     
  2. jcsd
  3. Nov 18, 2009 #2

    Mark44

    Staff: Mentor

    If a, f1, and f2, are all arrays of the same size, that should work. I would give a different name to a, though, something like diff.
     
  4. Nov 18, 2009 #3
    thats a good point and also my problem.
    I mean, if i have a function f1: y1=t^2+1, i can plot the graph like this:
    t=0:0.1:10
    y1=t.^2+1
    plot(t,y1)

    and f2 is an algorithm to approximately plot the function, as in it uses numerically methods. so, it depends on the mesh size, so if i pick an n, say n=50, i can plot the graph, and i can see that the graphs are similar.
    so the arrays of f1 and f2 wont necessarily be of the same size.
    yet i still need the difference.
    Is there a way to choose the t interval such that i can make them the same size?
     
  5. Nov 18, 2009 #4

    Pythagorean

    User Avatar
    Gold Member

    one way is to go back after you've made f2 and redefine f1 by redefining t:

    t= 0:1/length(f2):length(f2)

    or you may have to do:
    t = 1/length(f2):1/length(f2):length(f2)

    then

    f1 = (some function of t)
     
  6. Nov 18, 2009 #5
    I have defined the function f1 as:
    t=0:1/(length(f2(10))):length(f2(10));
    y=1+t.^2;
    plot(t,y)

    which works. then i plot f2(10) where the 10 is chosen for the mesh size, which also works. then i do:
    diff=f1-f2(10)
    now im getting the error:

    ??? Attempt to execute SCRIPT f1 as a function:
     
    Last edited: Nov 18, 2009
  7. Nov 18, 2009 #6

    Mark44

    Staff: Mentor

    f2(10) is just one element of your array, whereas f2 represents the entire array. Also notice that your assignment statement for t is different from what Pythagorean had. In one of his versions it was
    Code (Text):
    t= 0:1/length(f2):length(f2)
     
    In yours it is
    Code (Text):
    t=0:1/(length(nyst(10))):length(nyst(10));
     
     
  8. Nov 18, 2009 #7
    I corrected it,
    thanks for pointing it out.
     
  9. Nov 19, 2009 #8

    Mark44

    Staff: Mentor

    Is everything copacetic now?
     
  10. Nov 19, 2009 #9
    no, because the arrays are still different sizes.
     
  11. Nov 19, 2009 #10

    Mark44

    Staff: Mentor

    OK, here's a thought. Suppose your y1 array has 100 elements and your y2 array has 400 elements. You can create a new array for some of the y2 data, but with only 100 elements.
    Code (Text):

    for i=4:4:400
      y2_new(i/4) = y1(i)
    end
     
    I think this will work. The idea is that y1(4) --> y2_new(1), y1(8) --> y2_new(2), ..., y1(400) --> y2_new(100). After that you can plot the values of y1 and y2_new, and both arrays are the same size.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Graphs in matlab
  1. Matlab 3d graph (Replies: 1)

  2. Graphs in matlab (Replies: 2)

Loading...