MATLAB Graphing in MATLAB: Suitable Domain for f & g

  • Thread starter Thread starter roam
  • Start date Start date
  • Tags Tags
    Graphing Matlab
AI Thread Summary
To graph the functions f = (2+x)/((x-2)(x+1)) and g = 1/sqrt((2x^2 - 1)(x^2-1)) in MATLAB, a suitable domain must exclude the points where the functions are undefined. The domain for f is all real numbers except 2 and -1, while g is defined for x in the range [-2, ∞) excluding ±1. The correct MATLAB code to plot these functions involves creating a vector for x values and ensuring element-wise operations with the dot notation. Specifying the axis limits with the command axis([xmin xmax ymin ymax]) is crucial for visualizing the graph effectively. Choosing an appropriate domain enhances the clarity of the graph, especially since automatic range settings may not work well for these functions.
roam
Messages
1,265
Reaction score
12
Hi!

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

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
I also need to mention that when I'm trying to graph g=\frac{1}{\sqrt{(2x^2 - 1)(x^2-1)}} 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?
 
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)
 
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:
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
 
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:
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.
 

Similar threads

Replies
2
Views
2K
Replies
8
Views
2K
Replies
10
Views
3K
Replies
1
Views
4K
Replies
6
Views
2K
Back
Top