What is causing the clumping in my piecewise function plot?

Click For Summary
SUMMARY

The discussion centers on plotting a piecewise function in MATLAB, specifically addressing issues with clumping in the graph near x = -30. The user implemented a loop to generate y-values based on defined conditions but experienced unexpected behavior in the plot. The solution proposed involves reducing the increment from 0.05 to 0.001 to improve the sampling of the waveform and mitigate the clumping effect caused by linear interpolation.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of piecewise functions
  • Knowledge of plotting techniques in MATLAB
  • Basic concepts of numerical interpolation
NEXT STEPS
  • Explore MATLAB's interpolation functions for better plotting accuracy
  • Learn about the effects of sampling rates on graphical representations
  • Investigate piecewise function implementations in other programming languages
  • Study advanced plotting techniques in MATLAB, such as using 'plot3' for 3D visualizations
USEFUL FOR

Mathematics students, MATLAB users, and anyone interested in graphical data representation and numerical methods in programming.

STEMucator
Homework Helper
Messages
2,076
Reaction score
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
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.
 

Similar threads

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