MATLAB Mastering Matlab: Graphing Functions Made Easy

AI Thread Summary
The discussion focuses on using the MATLAB command "plot" to create two-dimensional graphs by connecting points with straight line segments. It explains how to define vectors for both x and y values and provides examples of plotting simple functions, such as f(x) = x^2. The initial attempt to graph this function is noted to be limited, prompting a suggestion to define a broader domain to visualize the function effectively over the interval [-10, 10]. Further, the discussion explores graphing a shifted quadratic equation, f(x) = (x - 10)^2, using a specified interval to capture the full graph. The conversation hints at future topics including differential equations and phase plots. Additionally, resources for further learning are shared, including a comprehensive Udemy crash course and MATLAB's online service, as well as a free alternative called Freemat for exploring MATLAB features.
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

  • Simple_function.jpg
    Simple_function.jpg
    11.2 KB · Views: 186
  • simple_func1.jpg
    simple_func1.jpg
    11.3 KB · Views: 203
  • func1.jpg
    func1.jpg
    10.1 KB · Views: 196
  • good1.jpg
    good1.jpg
    12.3 KB · Views: 184
  • 5_15.jpg
    5_15.jpg
    11.1 KB · Views: 204
Physics news on Phys.org
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/
 

Similar threads

Replies
4
Views
1K
Replies
4
Views
2K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
5
Views
3K
Replies
2
Views
3K
Back
Top