Finding the Difference Between Graphs in MATLAB

  • Thread starter Thread starter sara_87
  • Start date Start date
  • Tags Tags
    Graphs Matlab
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
9 replies · 4K views
sara_87
Messages
748
Reaction score
0

Homework Statement



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.

Homework Equations





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
 
Physics news on Phys.org
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 won't 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?
 
sara_87 said:
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 won't 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?

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)
 
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 I am getting the error:

? Attempt to execute SCRIPT f1 as a function:
 
Last edited:
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:
t= 0:1/length(f2):length(f2)

In yours it is
Code:
t=0:1/(length(nyst(10))):length(nyst(10));
 
I corrected it,
thanks for pointing it out.
 
no, because the arrays are still different sizes.
 
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:
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.