Finding the Difference Between Graphs in MATLAB

  • Thread starter Thread starter sara_87
  • Start date Start date
  • Tags Tags
    Graphs Matlab
AI Thread Summary
To find the difference between two graphs in MATLAB, ensure that the arrays representing the functions y1 and y2 are of the same size. If the functions are defined over different intervals or mesh sizes, adjust the time variable t for both functions to create compatible arrays. One method is to redefine t based on the length of the smaller array, ensuring both y1 and y2 have the same number of elements. Alternatively, you can create a new array from y2 that matches the size of y1 by selecting specific elements. This approach allows for the calculation of the difference without encountering size mismatch errors.
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
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.
 
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.
 
Is everything copacetic now?
 
no, because the arrays are still different sizes.
 
  • #10
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.
 

Similar threads

Replies
10
Views
2K
Replies
1
Views
2K
Replies
11
Views
6K
Replies
5
Views
2K
Replies
10
Views
1K
Replies
4
Views
1K
Replies
6
Views
2K
Back
Top