Graphing in MATLAB: Suitable Domain for f & g

In summary: To specify the domain you would do,xx = [-2,∞)\{±1} g = -1./sqrt((2*xx.^2-1).*(x.^2-1));plot(xx,g)In summary, the domain for g is all the real numbers except for 2 & -1, while the domain for f is all the real numbers except for 2 & -1 and -2.
  • #1
roam
1,271
12
Hi!

For those of you who know how to use Matlab, I want to graph two functions f = [tex]\frac{(2+x)}{(x-2)(x+1)}[/tex] and g = [tex]\frac{1}{\sqrt{(2x^2 - 1)(x^2-1)}}[/tex]

On my instructions sheet it says; draw f and g on the same grid on a "suitable" domain.

The domain of f is all the real numbers except for 2 & -1

The domain of g = [-2,∞)\{±1}

What is a suitable domain for both functions to put on the graph AND what's the code for specifying the domain in MATLAB?

could it be: x≥-2\±1 ∩ x = R\{2,-1}?
 
Last edited:
Physics news on Phys.org
  • #2
I also need to mention that when I'm trying to graph g=[tex]\frac{1}{\sqrt{(2x^2 - 1)(x^2-1)}}[/tex] I get errors. My code was;

>> syms x
>> ezplot 1/sqrt[(2x^2-1)*(x^2-1)]

I'm sure this is the right code for it so what's wrong?
 
  • #3
In Matlab, everything is vectors of numbers. So, you might do,

xx = -2:.1:10;
g = 1./sqrt((2*xx.^2-1).*(x.^2-1));
plot(xx,g)
axis([-2 4 -2 6])

This basically says,
1) make a list of numbers called xx starting at -2, increasing by .1 each increment until 10. ie: -2, -1.9, -1.8, etc

2) Make a list of numbers called g whose first value is the equation applied to the first element of xx, second value is the equation applied to the second element of xx, etc. The reasin there are little period dots in there (.* instead of *, ./ instead of /, .^2 instead of ^2), is that tells MATLAB to do the calculations element by element. Otherwise it might interpret xx*xx to mean take the dot product between the vectors or something like that. Adding the .'s makes sure any operation stays elementwise.

3) plot the list of xx coordinates against the list of g values

4) change the x and y mins and maxes on the graph for a better picture. (xmin = -2, xmax = 4, ymin = -2, ymax = 6)
 
  • #4
Btw, when you write "g = 1./sqrt((2*x.^2-1).*(x.^2-1))", what's the point of having a dot after the x and 1? like 1. & x.


The graph now looks something like http://img523.imageshack.us/img523/5479/40023260kg1.gif" Although the instruction is to draw it on a suitable domain... how do I do that?
 
Last edited by a moderator:
  • #5
It's not after the 1, but rather before the division sign: ./
Same thing for the x, its not after the x, its before the exponent sign: .^

By the way typo above the single x should be xx, or whatever you name it.

A suitable domain means you choose where to zoom in on the graph by choosing the bounding box limits for x and y
 
  • #6
What's the role of the dot that comes before the division sign/exponent sign?

So, what "suitable domain" would you think is right to choose? What are the codes for doing this?
 
Last edited:
  • #7
Normally a/b means to the matrix "division". Ie: a*(b inverse). What you want is to divide element by element, so that you put the dot in there to tell MATLAB so. Same thing with exponentiation.

The way to choose the domain is specifing the minimum and maximum x and y values for the graph, which you do with the code:
axis([xmin xmax ymin ymax])

Matlab will automatically guess these values if you don't specify them, but for this function automatic range doesn't work well.
 

FAQ: Graphing in MATLAB: Suitable Domain for f & g

What does the domain of a graph in MATLAB refer to?

The domain of a graph in MATLAB refers to the set of all input values for which the function is defined. It determines the range of values that can be used to plot the function.

How do I choose a suitable domain for my functions in MATLAB?

To choose a suitable domain for your functions in MATLAB, you should first consider the behavior of the function. For example, if the function has a vertical asymptote, the domain should not include that value. You can also use the "xlim" function in MATLAB to specify the range of values for the x-axis.

Can I plot multiple functions with different domains on the same graph in MATLAB?

Yes, you can plot multiple functions with different domains on the same graph in MATLAB. You can use the "hold on" command to plot multiple functions on the same figure. However, make sure that the domains do not overlap, as this can cause confusion in interpreting the graph.

How do I adjust the domain of a graph in MATLAB?

To adjust the domain of a graph in MATLAB, you can use the "xlim" and "ylim" functions. These allow you to specify the range of values for the x-axis and y-axis, respectively. You can also use the "axis" function to set the limits for both axes simultaneously.

Can I change the domain of a graph after it has been plotted in MATLAB?

Yes, you can change the domain of a graph after it has been plotted in MATLAB. You can use the "xlim" and "ylim" functions to adjust the domain, or you can use the "axis" function to set the limits for both axes simultaneously. Additionally, you can use the "gca" function to get the current axes and then modify the domain using its properties.

Similar threads

Replies
2
Views
1K
Replies
8
Views
861
Replies
10
Views
2K
Replies
2
Views
3K
Replies
1
Views
779
Replies
1
Views
4K
Back
Top