What is causing the clumping in my piecewise function plot?

In summary, the conversation is about plotting a piecewise function in MATLAB and the issues encountered with the resulting plot. The code used to plot the function is shown, and the question is raised about the strange behavior near x = -30. The issue is identified as an artifact of sampling and a suggestion is made to use a smaller increment to improve the plot.
  • #1
STEMucator
Homework Helper
2,076
140

Homework Statement



Hey there. I'm trying to plot a piecewise function in MATLAB and I'm having some decent success, but there's some things I'm wondering.

Here's the function: http://gyazo.com/d3493a0c13096878acf5a501af8a7f66


Homework Equations





The Attempt at a Solution



My strategy was to create an empty array to hold all the y values since plotting the x values is easy enough. I came up with this short snippet of code:

Code:
function plotFunction()
y = []; %Empty array to hold the y values

for i=-40:0.05:30
    if i <= 0
        y(end+1) = 38/11 + sin(i^2);
    elseif i <= 9
        y(end+1) = 38/(11-i);
    else
        y(end+1) = 1.5*sqrt(4*i) + 10;
    end
end

plot(-40:0.05:30, y);
title('Piecewise Function Plot');
xlabel('x-values');
ylabel('y-values');

end

This code yields the following plot, which is really close, but there's something wrong:

http://gyazo.com/1ef70c9ccf6c44f81c14d9915c9edf86

What's with the funky activity near ##x = -30##? Why does it get all clumped up around there?

I tried tinkering with the increment of 0.05, but shrinking it clumps the graph together even more.
 
Physics news on Phys.org
  • #2
Zondrina said:
What's with the funky activity near ##x = -30##? Why does it get all clumped up around there?
It's an artifact of sampling the waveform and plot doing linear interpolation. Try, for instance, to use an increment of 0.001 instead.
 

FAQ: What is causing the clumping in my piecewise function plot?

What is a piecewise function in MATLAB?

A piecewise function in MATLAB is a mathematical function that is defined by different equations or expressions for different intervals of the independent variable. This allows for more complex and specific mathematical models to be created.

How do you define a piecewise function in MATLAB?

To define a piecewise function in MATLAB, you can use the "piecewise" function or the "if-else" statement. The "piecewise" function allows you to define the function using multiple equations or expressions for different intervals, while the "if-else" statement allows you to specify conditions for each interval and the corresponding equation or expression.

What are the advantages of using piecewise functions in MATLAB?

Piecewise functions in MATLAB allow for more flexibility in modeling complex mathematical functions. They also make it easier to incorporate conditions and constraints into the function, as well as plot and analyze the function using built-in MATLAB functions and tools.

Can a piecewise function be used for interpolation?

Yes, a piecewise function can be used for interpolation. You can use the "interp1" function in MATLAB to interpolate values for a piecewise function given a set of data points. This can be useful for creating a smooth curve from a set of discrete data points.

How do you plot a piecewise function in MATLAB?

To plot a piecewise function in MATLAB, you can use the "ezplot" function or the "plot" function. The "ezplot" function is useful for quickly plotting a simple piecewise function, while the "plot" function allows for more customization and control over the plot. You can also use the "hold on" command to plot multiple piecewise functions on the same graph.

Similar threads

Replies
10
Views
2K
Replies
1
Views
1K
Replies
7
Views
1K
Replies
5
Views
2K
Replies
8
Views
850
Replies
10
Views
1K
Back
Top