Plotting a Function Against Its Argument in MATLAB

Click For Summary

Discussion Overview

The discussion revolves around how to plot functions in MATLAB, specifically focusing on plotting a function against its argument without defining discrete points on the x-axis. Participants explore the concept of continuous plotting and the limitations of MATLAB in representing real numbers.

Discussion Character

  • Homework-related, Technical explanation, Conceptual clarification, Debate/contested

Main Points Raised

  • One participant describes their function y = 3x + 2 and expresses a desire to plot it against its argument rather than a vector.
  • Another participant explains that plotting y = f(x) involves ordered pairs (x, f(x)), suggesting that the original question may not be clear.
  • A participant clarifies their intent to plot functions like sin(x) over a continuous range, rather than discrete points.
  • Some participants suggest using more points to create a smoother graph, while others emphasize that MATLAB requires discrete arrays for plotting.
  • One participant questions the possibility of plotting a function with continuous values on the x-axis without defining discrete points.
  • Another participant mentions the use of the linspace function to generate a specified number of points for smoother curves.
  • A later reply discusses the limitations of computers in representing real numbers, noting that they use rational numbers and thus cannot achieve true continuity in plotting.
  • One participant introduces a different plotting scenario involving 3D data and questions how to implement it in MATLAB.

Areas of Agreement / Disagreement

Participants generally agree that MATLAB requires discrete points for plotting, but there is some disagreement about the interpretation of continuous plotting and the use of functions like linspace. The discussion remains unresolved regarding the possibility of plotting functions with truly continuous values on the x-axis.

Contextual Notes

Limitations include the dependence on discrete representations of real numbers in MATLAB, which affects how functions can be plotted. The discussion also highlights the need for finite arrays in plotting functions.

sara_87
Messages
748
Reaction score
0

Homework Statement



I have a function y where y = 3x+2
I made x as a vector in matlab:
for i=1:10
x(i) = 4*i

but when i do a plot, i don't want to plot y against the vector x but against x as an argumetn of the function.
In general if i have any function...how do i plot it against its argument (not the argument as a vector)
Thank you

Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
When you plot y = f(x) each point in your graph is an ordered pair (x, f(x)), so you are plotting a function value against its input argument.

I might not be understanding what you're asking. The only other thing I can think of is that maybe you want to plot y1 = f(x) together with y2 = x. The graph of the latter is a straight line through the origin and whose slope is 1.
 
Sorry, my explanation wasn't very clear.
I want the function to be plot for continuous values on the x axis.
For an example, if i want to plot sin(x), i will do:
x=[0,pi/10,5*pi]
y=sin(x)
plot(x,y)

this will return the function sinx with 0, pi/10, 2pi/10, 3pi/10,...5*pi on the x axis.

But what if i want to plot y=sin(x) from 0 to 5pi with the x-axis being not discrete points but continuous points.

How would i do this?

Thank you
 
I'm still not sure I understand what you're asking. Your x-values are always going to be discrete, but if you use more points, they will be closer together. In your example you are plotting 51 points, with the x-values being pi/10 units apart. If you plot 10 times as many points, the points that are graphed will be closer together.

x=[0,pi/100,5*pi]
y=sin(x)
plot(x,y)
 
I am trying to find a way in Matlab to plot the sin(x) curve without defining discrete points.
I just want the sine curve from 0 to 5pi without defining how many points on the x-axis. is there a way to do this?
 
I don't think there is. To use the plot function you need two arrays: one for the x-axis and one for the y-axis. Necessarily the arrays will have a finite number of elements, so the values will be discrete.
 
that's what i thought. but i thought that there was a way to plot a function (with respect to its argument) with the continuous points on the x axis.
 
Another approach similar to mark44's is to use the linspace function but that's just increasing the size of the x vector and letting Matlab handle the size of the partitions.

x = linspace(0,5*pi,50)
y = sin(x)
plot(x,y)

In the linspace function, you can define how many points you would like to make the vector with the last argument, in this case 50 points. It's been a while since I've plotted but this amount of arguments should give a smooth curve.
 
Thanks.
so i guess i can't plot the function on MATLAB with continuous values on the x axis?
 
  • #10
Computers don't really work with the real numbers of mathematics; they use rational numbers only. In a programming language (including matlab), the representation for a real number is stored in a relatively small amount of storage, typically 4 or 8 bytes and sometimes 10 bytes. This means that real numbers as represented in computers are discrete, with spaces between adjacent numbers. Unlike the real numbers of mathematics, it's not necessarily true for computers that you can always find another real number between any two real numbers.
 
  • #11
I need to plot
x=linspace(0,1)
p=x^3
y=linspace(-1,1)
plot(x,y,p)------?
but I need this data to be in 3 dim with different recangular fig...
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K