Plotting functions in MATLAB with custom domains

1 reply · 5K views · last reply
DrWahoo
Messages
45
Reaction score
0
The MATLAB command plot plots points in a two-dimensional figure and connects
them with straight line segments. So, here $y$ is a vector with $n$ entries
$(1; y(1)); (2; y(2)); (3; y(3)); : : : ; (n; y(n)):$

So the MATLAB command is given below for an example:
Code:
y = [1 -3 3 -4];
>> plot(y)

Yields the output here:
View attachment 7555

We can also define them in function form where each $x$ value corresponds to an output- $y$ value.
Code is given below;
Code:
y = [10 2 -6 3 -5]; x=[1 3 5 7 10];
>> plot(x,y)
Matlab output is below;
View attachment 7556Now let's try a simple function; let's graph $f(x)=x^2$
We first try using the following code;
Code:
y=x.^2;
plot(x,y);
Then we get the following output. Does this look strange?
View attachment 7557
This graph doesn't show me anything about the function for positive $x$ values? Can we fix this?
Yes, recall MATLAB is primarily based off matrices hence the "mat" lab. Aka Matrix labratories.

So let's define the values of the domain we want to see
Say for instance we want to look at the graph of the function on the interval $[-10, 10]$
We would use the following code
Code:
x=-10:0.1:10;
y=x.^2
plot(x,y)
To get the desired output below;
View attachment 7560So what if we want to graph a simple quadratic equation that is shifted to the left 10 points?
So let's start with the function $f(x)= x^2$ , shifting it to the right 10 units, we have $f(x)=(x-10)^2$
Now let's try to graph this. We could use points, but let's see if we can get the full graph. Hence, we shall try the following code knowing the x coordinate of the vertex is at 10, so we use the interval $[5,15]$
Code:
x=5:0.1:15;
y=(x-10).^2
plot(x,y)

The desired output is given below;
View attachment 7561This will continue into differential equations, phase plots, and gradient fields. Will update shortly.
 
Attachments
  • MATLAB plot of a piecewise-linear line through points (1, 1), (2, -3), (3, 3) and (4, -4), with x-axis from 1 to 4 and y-axis from -4 to 3.
    Simple_function.jpg
    11.2 KB · Views: 248
  • MATLAB plot of a piecewise linear graph joining points (1, 10), (5, -6), (7, 3) and (10, -5) on axes from 1 to 10 (x) and -6 to 10 (y).
    simple_func1.jpg
    11.3 KB · Views: 268
  • MATLAB plot of a decreasing curve from (-10, 100) to about (-3, 9), with the x-axis labelled -10 to -3 and the y-axis labelled 0 to 100 in steps of 10.
    func1.jpg
    10.1 KB · Views: 258
  • MATLAB plot of y = x^2 over the domain from -10 to 10, a U-shaped parabola with its vertex at the origin and the curve reaching 100 at both ends.
    good1.jpg
    12.3 KB · Views: 268
  • MATLAB plot of a parabola opening upward with its vertex at (10, 0), over x from 5 to 15 and y from 0 to 25, with the curve reaching 25 at both ends.
    5_15.jpg
    11.1 KB · Views: 274
Udemy has a more comprehensive and free crash course for those of us who need more training.

https://www.udemy.com/course/matlab-crash-course/?utm_source=adwords&utm_medium=udemyads&utm_campaign=DSA_Catchall_la.EN_cc.US&utm_content=deal4584&utm_term=_._ag_95911180068_._ad_532194018659_._kw__._de_c_._dm__._pl__._ti_dsa-52949608673_._li_9028292_._pd__._&matchtype=&gclid=CjwKCAjw9suYBhBIEiwA7iMhNHX-_OgKEGjJd3P_Mpb40MVumJQsCawf_W6G97sHXJhQAIQmTDx9rhoCNfQQAvD_BwE

and this Youtube course:



Matlab provides an online service for running MATLAB code without the software. I think you have to signup and pay a subscription fee to do so.

https://www.mathworks.com/products/matlab-online.html

In addition there is a free Matlab clone called Freemat that provides an easy way to explore MATLAB features.

https://en.wikipedia.org/wiki/FreeMat

http://freemat.sourceforge.net/