Finding the Difference Between Graphs in MATLAB

  • Thread starter Thread starter sara_87
  • Start date Start date
  • Tags Tags
    Graphs Matlab
Click For Summary

Discussion Overview

The discussion revolves around finding the difference between two graphs plotted in MATLAB, specifically when the functions return arrays of different sizes. Participants explore methods to align the arrays for subtraction and address errors encountered during the process.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant describes their attempt to find the difference between two functions, f1 and f2, in MATLAB, and encounters an error when trying to subtract them directly.
  • Another participant suggests that if f1 and f2 are arrays of the same size, the subtraction should work, recommending a different variable name for clarity.
  • A participant explains that due to the numerical methods used for f2, the arrays may not be of the same size, raising the question of how to choose the t interval to make them compatible.
  • One suggestion involves redefining the t variable after creating f2 to ensure both arrays are the same size.
  • A participant shares their definition of f1 and the plotting process, but encounters an error related to treating f1 as a function instead of an array.
  • Another participant points out that f2(10) refers to a single element, while f2 represents the entire array, highlighting a potential source of confusion.
  • One participant proposes creating a new array from y2 data to match the size of y1, suggesting a method to select elements at regular intervals.

Areas of Agreement / Disagreement

Participants express various methods to align the sizes of the arrays for subtraction, but there is no consensus on a single solution, and some participants continue to face issues with array sizes.

Contextual Notes

Participants discuss specific MATLAB syntax and functions, but there are unresolved details regarding the definitions of the functions and the implications of numerical methods on array sizes.

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 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 11 ·
Replies
11
Views
6K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K