Finding the Difference Between Graphs in MATLAB

In summary, the conversation revolves around finding the difference between two functions in MATLAB, y1 and y2, which have different array sizes. The solution suggested is to redefine the t interval of the smaller array to match the larger one, and then use a for loop to create a new array with the same number of elements. This will allow for a proper comparison of the two functions.
  • #1
sara_87
763
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
  • #2
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.
 
  • #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 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?
 
  • #4
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)
 
  • #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 I am getting the error:

? Attempt to execute SCRIPT f1 as a function:
 
Last edited:
  • #6
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));
 
  • #7
I corrected it,
thanks for pointing it out.
 
  • #9
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.
 

1. How do I plot two graphs in MATLAB and find the difference between them?

To plot two graphs in MATLAB, use the "plot" function and pass in the x and y values for each graph. To find the difference between the two graphs, simply subtract one graph from the other using the "-" operator.

2. Can I plot more than two graphs and find the difference between them in MATLAB?

Yes, you can plot multiple graphs in MATLAB and find the difference between them. Simply plot each graph using the "plot" function and then use the "-" operator to subtract the graphs from each other.

3. How do I customize the appearance of the difference graph in MATLAB?

You can customize the appearance of the difference graph by using various plot formatting functions such as "xlabel", "ylabel", "title", and "legend". You can also change the line style, color, and thickness using the "LineStyle", "Color", and "LineWidth" parameters in the "plot" function.

4. Is there a built-in function in MATLAB to find the difference between graphs?

Yes, there is a built-in function in MATLAB called "diff" which can be used to find the difference between two graphs. It takes in the x and y values of the graphs as parameters and returns the difference between them.

5. Can I save the difference graph as an image file in MATLAB?

Yes, you can save the difference graph as an image file in MATLAB by using the "saveas" function. Simply pass in the figure handle and the desired file format (e.g. "png", "jpg") as parameters to the function.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
882
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Calculus and Beyond Homework Help
Replies
10
Views
987
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top