Computing numerical derivative with gnuplot - plot

I've been trying to compute numerically the derivative using gnuplot, using the scripts in this other discussion, even with the same data file. However I keep getting this error:
gnuplot> d(y) = ($0 == 0) ? (y1 = y, 1/0) : (y2 = y1, y1 = y, y1-y2)
^
"prova.g", line 7: ')' expected
I don't know what to do here. Any help?

Here is an example for numerical derivatives from my collection. Requires gnuplot >=5.0 and with the use of files instead of datablocks (and probably with some tweaking with gnuplot>=4.4.0).
Script: (works with gnuplot>=5.0.0, Jan. 2015)
### numerical derivatives
reset session
# create some data
MyFunction = "sin(x)/x"
set table $Data
set samples 150
plot [-10:10] '+' u 1:(#MyFunction) w table
unset table
DerivX(colX) = (x0=x1,x1=column(colX),(x0+x1)/2.)
DerivY(colY) = (y0=y1,y1=column(colY),(y1-y0)/(x1-x0))
set table $Deriv1
plot x1=y1=NaN $Data u (DerivX(1)):(DerivY(2)) w table
unset table
set table $Deriv2
plot x1=y1=NaN $Deriv1 u (DerivX(1)):(DerivY(2)) w table
unset table
set table $Deriv3
plot x1=y1=NaN $Deriv2 u (DerivX(1)):(DerivY(2)) w table
unset table
plot $Data u 1:2 w l lc rgb "red" ti MyFunction, \
$Deriv1 u 1:2 w l lc rgb "web-green" ti "1st Derivative", \
$Deriv2 u 1:2 w l lc rgb "blue" ti "2nd Derivative", \
$Deriv3 u 1:2 w l lc rgb "magenta" ti "3rd Derivative"
### end of script
Result:
Addition: (version for gnuplot 4.2.6, Sept. 2009)
gnuplot 4.2.6 doesn't have datablocks and serial evaluation, but here is a cumbersome workaround without these features.
for illustration, the script creates a data file SO68198576.dat (you already have your input file)
plot the data file into another file TEMP1 skipping the first data line
merge the files line by line into another file TEMP2 using the system command paste (either on Linux already on the system or on Windows you have to install, e.g. CoreUtils from GnuWin).
now you can calculate dx and dy between two successive datapoints from column 1 and 4 and column 2 and 5, respectively.
since the files have different length, the last line(s) should be skipped. This can be done by the system command head -n -2.
That's how TEMP2 looks like:
#Curve 0 of 1, 150 points #Curve 0 of 1, 150 points
#x y type #x y type
-10 -0.0544021 i -9.86577 -0.0432646 i
-9.86577 -0.0432646 i -9.73154 -0.0310307 i
-9.73154 -0.0310307 i -9.59732 -0.0178886 i
...
Script: (works with gnuplot 4.2.6, requires system commands paste and head)
### numerical derivative for gnuplot 4.2.6
reset
FILE = "SO68198576.dat"
set table FILE
set samples 150
plot sin(x)/x
unset table
TEMP1 = FILE.'1'
TEMP2 = FILE.'2'
set table TEMP1
plot FILE u 1:2 every ::1
unset table
system(sprintf('paste %s %s > %s', FILE, TEMP1, TEMP2))
system(sprintf('head -n -2 %s > %s',TEMP2, TEMP1))
x0(col) = (column(col)+column(col+3))/2.
dx(col) = (column(col+3)-column(col))/2.
dy(col) = (column(col+3)-column(col))/2.
plot FILE u 1:2 w lp pt 7 title "Data", \
TEMP1 u (x0(1)):(dy(2)/dx(1)) w lp pt 7 title "1st Derivative"
### end of script
Result: (screenshot gnuplot 4.2.6)

Related

gnuplot -- plot range different from axes range

I have this very simple, working gnuplot code:
splot x * x
This plots a surface from -10 < x < 10 and -10 < y < 10.
I want to plot the same surface from -10 < x < 10 and -10 < y < -9 HOWEVER I still want the graph's axes to use -10 < x < 10 and -10 < y < 10. The axes bounds should not change, but the range of values plotted should be smaller.
In other words, the surface should look like a thin band in one small section of the plot. The rest should be empty space. Below is a very poor effort at creating what I want using the graph I have.
I have tried manipulating yrange, but this changes BOTH the surface bounds and the axes bounds. I have also tried disabling autoscale but I'm unsure what to do next.
In current gnuplot (version 5.2.4) you can separate the sampling range from the axis range whether or not you enable parametric mode. See example in the on-line demo set:
sampling.html (see plot #9)
One way to do this is through parametric mode, which gives you a separate set of coordinates (u and v) to set ranges for
f(x,y) = x*x
set xrange [-10:10]
set yrange [-10:10]
set urange [-10:10]
set vrange [-10:-9]
set parametric
splot u, v, f(u,v)
Alternatively you can achieve the same effect by using the ++ special filename instead of parametric mode:
f(x,y) = x*x
set xrange [-10:10]
set yrange [-10:10]
set urange [-10:10]
set vrange [-10:-9]
splot "++" u 1:2:(f($1,$2)) w l
Using a programming language with gnuplot might give better control over plots.
Here is an example using c and a script.
plot.c
#include <stdio.h>
#include <math.h>
int main()
{
int x, y;
double z;
for (y=-10; y <= 10; y++)
{
for (x=-10; x <= 10; x++)
{
z = x * 0.025;
printf("%f %f %f\n", (double)x, (double)y, 100 - cos(z * M_PI*2) * 100);
}
printf("\n");
}
fflush(stdout);
return 0;
}
plot.bat
gcc plot.c -o prog.exe -lm
prog > data.txt
echo splot "data.txt" using 1:2:3 with lines | gnuplot -p
plot.sh
gcc plot.c -o prog -lm
./prog > data.txt
echo 'splot "data.txt" using 1:2:3 with lines' | gnuplot -p
Here is an example using python and a script.
It will output:
plot.py
import math
print("\"Surface One\"")
for y in range(-10, 10+1):
for x in range(-10, 10+1):
z = x * 0.025
print("%f %f %f 1" % (float(x), float(y), 100 - math.cos(z * math.pi*2) * 100))
print("")
print("")
print("\"Surface Two\"")
for y in range(-10, -8+1):
for x in range(-10, 10+1):
z = x * 0.025
print("%f %f %f 2" % (float(x), float(y), 100 - math.cos(z * math.pi*2) * 100))
print("")
print("")
print("\"Surface Three\"")
for y in range(-10, 10+1):
for x in range(-10, 10+1):
z = x * 0.1
print("%f %f %f 7" % (float(x)/2, float(y)/2, math.sin(z * math.pi*2) * 10))
print("")
print("")
plot.bat
cd %~dp0
python plot.py > data.txt
echo ^
set terminal wxt size 570,420; ^
stats "data.txt" u 1:2 nooutput; ^
set border 0x7F linecolor rgb "#555555"; ^
set grid xtics linecolor rgb "#888888" linewidth 0.2 linetype 9; ^
set grid ytics linecolor rgb "#888888" linewidth 0.2 linetype 9; ^
set grid ztics linecolor rgb "#888888" linewidth 0.2 linetype 9; ^
set xrange [-10.0:10.0]; ^
set yrange [-10.0:10.0]; ^
set zrange [-10.0:100.0]; ^
splot for [IDX=0:STATS_blocks-1] "data.txt" i IDX using 1:2:3:4 with lines ^
lc variable title columnheader(1) | gnuplot -p
plot.sh
#!/bin/bash
python plot.py > data.txt
echo \
'set terminal wxt size 570,420; \
stats "data.txt" u 1:2 nooutput; \
set border 0x7F linecolor rgb "#555555"; \
set grid xtics linecolor rgb "#888888" linewidth 0.2 linetype 9; \
set grid ytics linecolor rgb "#888888" linewidth 0.2 linetype 9; \
set grid ztics linecolor rgb "#888888" linewidth 0.2 linetype 9; \
set xrange [-10.0:10.0]; \
set yrange [-10.0:10.0]; \
set zrange [-10.0:100.0]; \
splot for [IDX=0:STATS_blocks-1] "data.txt" i IDX using 1:2:3:4 with lines \
lc variable title columnheader(1)' | gnuplot -p
Example without explicit parametric
This was mentioned at: https://stackoverflow.com/a/51546029/895245 with a link to the docs, here is a minimal example:
set terminal png
set output 'splot-domain.png'
# Number of x and y samples.
set isosamples 10, 5
# Plotted domain.
set urange [-5.0 : 0.0]
set vrange [-5.0 : 5.0]
# Visible domain.
set xrange [-5.0 : 5.0]
set yrange [-5.0 : 5.0]
# Just to make plot look nicer.
set hidden3d
set xyplane at 0
set xlabel 'x'
set ylabel 'y'
splot '++' using 1:2:($2**2) with lines
GitHub upstream.
Output:
Tested on gnuplot 5.2 patchlevel 8.

Extract values from a file and average

I have a file which from which I'd like to extract two values (Time, C_F[6]) highlighted below. Its in a CentOS 7 environment so can use bash or gnuplot or r. I'm not even sure how to google that (e.g. extract values from file bash doesn't really come up with solutions). Is it possile?
I'd like to be able to:
plot Time vs C_F[6]
Average C_F[6]
EDIT 1:
I think this might be on the lines, but it reproduces the whole file
sed 's/^.*C_F[6]=//' C_F.pressure > outputfile
EDIT 2:
Extract of the file:
/*---------------------------------------------------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: 3.0.0 |
| \\ / A nd | Web: www.OpenFOAM.org |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
Build : 3.0.0-6abec57f5449
Exec : patchAverage p C_F -parallel
Date : Apr 15 2017
Time : 15:01:20
Host : "login2.jjj.uk"
PID : 59764
Case : /nobackup/jjjj/Silsoe/Solid/solid_0_LES/motorBikeLES
nProcs : 8
Slaves :
7
(
"login2.jjjj.59765"
"login2.jjjj.59766"
"login2.jjjj.59767"
"login2.jjjj.59768"
"login2.jjjj.59769"
"login2.jjjj.59770"
"login2.jjjj.59771"
)
Pstream initialized with:
floatTransfer : 0
nProcsSimpleSum : 0
commsType : nonBlocking
polling iterations : 0
sigFpe : Enabling floating point exception trapping (FOAM_SIGFPE).
fileModificationChecking : Monitoring run-time modified files using timeStampMaster
allowSystemOperations : Allowing user-supplied system call operations
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Create time
Create mesh for time = 0.18
Time = 0.18
Reading volScalarField p
Average of volScalarField over patch C_F[6] = -18.3176
Time = 0.19
Reading volScalarField p
Average of volScalarField over patch C_F[6] = -18.299
Time = 0.2
Reading volScalarField p
Average of volScalarField over patch C_F[6] = -18.2704
Time = 0.21
Reading volScalarField p
Average of volScalarField over patch C_F[6] = -18.2349
Here's a crude way to do things:
# extract text from file line by line; will be indexed by line
sample <- readLines("D:\\tempFiles/example.txt")
# index the lines contaning "Time = "
timeI <- grep(x = sample, pattern = "Time = ")
# index the lines contaning "C_F[6]"; note that \\ is escape for [ and ]
C_FI <- grep(x = sample, pattern = "C_F\\[6\\]")
# extract lines and clean them
# note that these lines only contain "Time = values"; so just remove the "Time = "
timeval <-as.numeric(gsub(x = sample[timeI], pattern = "Time = ", replacement = ""))
# extract lines and clean them
# note that gsub removes all characters from te start (^) until "= "
C_FIval <- as.numeric(gsub(x = sample[C_FI], pattern = "^.*= ", ""))
# plot timve vs CF[6]
plot(y = timeval, x = C_FIval )
# get the mean
mean(C_FIval)
There are more elegant ways for the regex, but I'm still finding my way through that. This should be a basic way.
Since the OP tagged also gnuplot, here is a platform independent gnuplot-only solution.
How it's done:
standard datafile separator is whitespace
the function getTime() will check if the first string column is equal to "Time" and at the same time the second stringcolumn must be '=' (because you have Time in the first column in the header as well). If this is true then column 3 is the time and you memorize this value in the variable t0.
the function getValue() will check if the first string column is equal to "Average" and at the same time the 6th stringcolumn must be "C_F[6]". If this is true then column 8 is the value and memorize this value in y0, sum it up in ySum and increase the counter c by 1. If it is false, the function's return value will be NaN and nothing will be plotted. Note, the function has to check the first column because in case there is no 6th column the check will fail.
calculate the average by yAvg=ySum/c and plot and print it into the graph.
You might notice that the plotted datapoints in the first plot are not connected although the plotting style with linespoints was used. The reason is that there are empty lines in the input file and gnuplot will interrupt curves at empty lines.
Hence, in case you want connected lines you have to remove these empty lines which you can do by plotting the file each line as a whole (set datafile separator "\n") into a datablock table (with table). This requires gnuplot>=5.2.0. Furthermore, by using set datafile missing NaN gnuplot will not interrupt lines at NaN values.
This extraction can easily be adapted to any other input data format.
Data: Save the OP's data example as SO43427046.dat
Script: (the first solution works with gnuplot>4.4.4, Nov. 2011 and the second solution with gnuplot>=5.2.0, Sept. 2017)
### extract specific data from a file
reset
FILE = "SO/SO43427046.dat"
getTime(col1,col2,col3) = strcol(col1) eq "Time" && strcol(col2) eq "=" ? t0=column(col3) : t0
getValue(col1,col2,col3) = strcol(col1) eq "Average" && strcol(col2) eq "C_F[6]" ? \
(y0=column(col3),ySum=ySum+y0,c=c+1,y0) : NaN
set key top left
set ytics 0.02
set multiplot layout 2,1
ySum = c = 0
t0 = y0 = NaN
plot FILE u (getTime(1,2,3)):(getValue(1,6,8)) \
w lp pt 7 lc rgb "red" ti "unconnected points", \
(yAvg=ySum/c) w l lc rgb "blue" ti sprintf("Average: %g",yAvg)
set table $Data
set datafile separator "\n"
plot FILE u (strcol(1)) w table
set datafile separator whitespace
unset table
set datafile missing NaN
ySum = c = 0
t0 = y0 = NaN
plot $Data u (getTime(1,2,3)):(getValue(1,6,8)) w lp pt 7 lc rgb "red" ti "linespoints", \
(yAvg=ySum/c) w l lc rgb "blue" ti sprintf("Average: %g",yAvg)
unset multiplot
### end of script
Result:

Plotting a chain of spheres with gnuplot

From a function in C++ I get in a file the coordinates of the centers of a chain of spheres (of constant radius r). I would like to plot this chain with gnuplot. How can I represent the spheres with the true radius? This solution actually does not work, since the unit of pointsize is not the same as that of the axis (and is also changing with the axis limits).
This a slightly dirty solution which uses parametric (and some commands from Unix). For each line of the following data, we will plot a sphere with radius r, and centered at (x,y,z):
# points.dat :
# x y z radius
0 0 0 0.5
1 2 2 1.0
3 4 5 0.7
2 5 7 1.0
1 3 4 0.75
2 0 1 1.5
In other words, we will run commands with the form:
splot x1+r1*cos(v)*cos(u), y1+r1*cos(v)*sin(u), z1+r1*sin(v) title "line 1",\
x2+r2*cos(v)*cos(u), y2+r2*cos(v)*sin(u), z2+r2*sin(v) title "line 2", ...
The following code will do the trick (comments through the script):
set view equal xyz # to scale the axes of the plot
set hidden3d front # draw opaque spheres
set parametric # enable parametric mode with angles (u,v)
set urange [0:2*pi]
set vrange [-pi/2.0:pi/2.0]
filename = 'spheres.dat'
# get number of data-lines in filename
nlines = system(sprintf('grep -v ^# %s | wc -l', filename))
# this will save the plot commands
commands = 'splot '
do for [i=1:nlines] {
# get the i-th line
line = system( sprintf('grep -v ^# %s | awk "NR == %i {print; exit}" ', filename, i) )
# extract the data
x = word(line,1)
y = word(line,2)
z = word(line,3)
r = word(line,4)
# and save the instructions to plot the corresponding sphere
commands = commands . sprintf('%s + %s*cos(v)*cos(u), %s + %s*cos(v)*sin(u), %s + %s*sin(v) t "line %i"', x, r, y, r, z, r, i)
# if not EOF, add a comma to commands
if(i<nlines) { commands = commands . ', ' }
}
# commands is a string. We can run it into the command line through macros
set macros
#commands
This is the output I obtain:

gnuplot change color of the connecting lines

I am using gnuplot for the following. I have n equations which I want to plot based on the xaxis value. Here is a sample
set xrange[0:25]
f1(x) = x
f2(x) = 3*x
f3(x) = 10*x
plot (x>0)&&(x<10)?f1(x):(x<20)?f2(x):f3(x)
I know that we can set the color of the line easily by using the below. But it changes the whole color
set style line 1 lt 1 lw 3 pt 3 lc rgb "blue"
But what I want is to make the connecting lines a different color. ie if you plot the above graph you will 5 lines. 3 original lines (from the function) and 2 lines (the almost vertical lines) connecting them. I want to change the color of the connecting lines.
Note 1: These functions are automatically generated by a program, and the number of functions could be large. Even the exact plot command is automatically generated
Note 2: I want a way to differentiate my original lines with the interpolated lines which joins my original lines.
Any help is appreciated
What you actually have is one line defined piecewise, and there isn't an easy way to define colors for line segments within a piecewise line in gnuplot.
Easy way (plot a data file)
I would recommend making a data file looking like this:
# x y color
0 0 0
10 10 0
10 10 1
10 30 1
10 30 0
20 60 0
20 60 1
20 200 1
20 200 0
25 250 0
Notice the double points at x=10 and x=20. This is so the line segments meet at the transitions.
Now plot it with linecolor variable:
#!/usr/bin/env gnuplot
reset
set terminal pdfcairo enhanced color dashed rounded lw 5 size 3,2 font 'Arial,14'
set output 'output2.pdf'
set style data lines
set key top left
set tics scale 0.5 out nomirror
plot 'data.dat' u 1:2:3 lc variable
It looks like this:
You can change the palette (set palette) to determine the colors, and you can have more than 2 color values in the data file if you want.
Harder way (only OK for few segments)
You could define 2n-1 separate lines and connect them:
#!/usr/bin/env gnuplot
reset
set terminal pdfcairo enhanced color dashed rounded lw 5 size 3,2 font 'Arial,14'
set output 'output.pdf'
set style data lines
set key top left
set tics scale 0.5 out nomirror
# points every 0.001 units in the range 0:25
set samples 25001
# main lines
f1(x) = (x <= 9.999) ? x : 1/0
f3(x) = (x >= 10.001) && (x <= 19.999) ? 3*x : 1/0
f5(x) = (x >= 20.001) ? 10*x : 1/0
# define slopes and y-offsets of connecting lines
m2 = (f3(10.001)-f1(9.999))/0.002
b2 = (30.0-10.0)/2.0 + 10.0
m4 = (f5(20.001)-f3(19.999))/0.002
b4 = (200.0-60.0)/2.0 + 60.0
# connecting functions
f2(x) = (x >= 9.999) && (x <= 10.001) ? m2*(x-10) + b2 : 1/0
f4(x) = (x >= 19.999) && (x <= 20.001) ? m4*(x-20) + b4 : 1/0
plot [0:25] f1(x), f2(x), f3(x), f4(x), f5(x)
Which looks like this:
You can define a secondary function to define the breakpoints of your function, which is automatically coloring the right linepiece. The below code is easy to extend to different functions and breakpoints (i.e., you can just change x1 or x2). Adding multiple points is also straightforward.
xmin=0.
xmax=25.
x0=0.
x1=10.
x2=20.
nsample=200.
dx=(xmax-xmin)/nsample
print dx
set xrange[xmin:xmax]
set sample nsample
f1(x) = x
f2(x) = 3*x
f3(x) = 10*x
f4(x) = (x>x0)&&(x<x1)?f1(x):(x<x2)?f2(x):f3(x)
f5(x) = x
f5(x) = ( (x>x1&&x<=x1+dx) || (x>x2&&x<=x2+dx) )?1:0
set cbrange [0:1]
unset key
plot '+' using 1:(f4($1)):(f5($1)) lc variable with lines
Not that I have use the special filename '+', which just constructs a data file with equally space datapoints (following nsample).
If it is ok to skip the connecting lines, then you can use a simplified version of #andyras second variant. Just define all functions to be 1/0 when outside a specified range:
set style data lines
unset key
f1(x) = (x > 0) && (x < 10) ? x : 1/0
f2(x) = (x > 10) && (x < 20) ? 3*x : 1/0
f3(x) = (x > 20) ? 10*x : 1/0
plot [0:25] f1(x), f2(x), f3(x)
Following yet another possibility. This assumes, that you can select a sampling high enough, so that the "jumps" which connect the functions are always greater than inside a function:
set style data lines
unset key
set xrange[0:25]
f1(x) = x
f2(x) = 3*x
f3(x) = 10*x
f(x) = ( (x>0)&&(x<10)?f1(x):(x<20)?f2(x):f3(x) )
set samples 1000
curr = 0
prev = 0
lim = 1
plot '+' using (prev = curr, curr=f($1), $1):(f($1)):(abs(curr-prev) < lim ? 0 : 1) lc var

Plotting evolution of 2D vector in 3D as a ribbon in MATLAB

I would like to plot how the amplitude and orientation of a 2D vector evolves over time. To do this I would like to create a graph reminiscent of the canonical E & B field graphs you may recall from an introductory electricity and magnetism class.
Specifically, I would like to connect my 2D vector points with a ribbon, so that they are easy to see. Is there a simple way to do this in MATLAB? quiver3 is pretty close, but it lacks the ribbon. Perhaps some sort of parametric surface?
You can use the plotting functions FILL3 and QUIVER3 to do something like this:
x = linspace(0,4*pi,30); %# Create some x data
y1 = sin(x); %# Create wave 1
y2 = sin(x-pi); %# Create wave 2
u = zeros(size(x)); %# Create a vector of zeroes
hRibbon1 = fill3(x,y1,u,'r'); %# Plot wave 1 and fill underneath with color
set(hRibbon1,'EdgeColor','r',... %# Change the edge color and
'FaceAlpha',0.5); %# make the colored patch transparent
hold on; %# Add to the existing plot
quiver3(x,u,u,u,y1,u,0,'r'); %# Plot the arrows
hRibbon2 = fill3(x,u,y2,'b'); %# Plot wave 2 and fill underneath with color
set(hRibbon2,'EdgeColor','b',... %# Change the edge color and
'FaceAlpha',0.5); %# make the colored patch transparent
quiver3(x,u,u,u,u,y2,0,'b'); %# Plot the arrows
axis equal; %# Use equal axis scaling
And here's the resulting plot:
here's a solution that draws a ribbon between any two lines in 3D space. you can plot your quiver over it & adjust the opacity using 'FaceAlpha' as in gnovice's solution
To make the function clearer, I am first posting it without error-checking and resizing functions (which make up most of the body of the function & aren't particularly interesting)
function h = filledRibbon (x,y,z,u,v,w,c, varargin)
%function filledRibbon (x,y,z,u,v,w,c, varargin)
%
%plots a ribbon spanning the area between the lines x,y,z and x+u,y+v,z+w
%in the color c
%varargin is passed directly to patch
%returns a handle to the patch graphic created
%make up a set of regions that span the space between the lines
xr = [x(1:end-1); x(1:end-1) + u(1:end-1); x(2:end) + u(2:end); x(2:end)];
yr = [y(1:end-1); y(1:end-1) + v(1:end-1); y(2:end) + v(2:end); y(2:end)];
zr = [z(1:end-1); z(1:end-1) + w(1:end-1); z(2:end) + w(2:end); z(2:end)];
%plot the regions with no edges
h = patch(xr,yr,zr,c, 'LineStyle','none', varargin{:});
use this error-checking version in your actual code:
function h = filledRibbon (x,y,z,u,v,w,c, varargin)
%function filledRibbon (x,y,z,u,v,w,c, varargin)
%
%plots a ribbon spanning the area between the lines x,y,z and x+u,y+v,z+w
%in the color c
%varargin is passed directly to patch
%returns a handle to the patch graphic created
if ~exist('w', 'var') || isempty(w)
w = 0;
end
if ~exist('u', 'var') || isempty(u)
u = 0;
end
if ~exist('v', 'var') || isempty(v)
v = 0;
end
if ~exist('c', 'var') || isempty(c)
c = 'b';
end
%make all vectors 1xN
x = reshape(x,1,[]);
y = reshape(y,1,[]);
z = reshape(z,1,[]);
%if any offsets are scalar, expand to a vector
if all(size(u) == 1)
u = repmat(u, size(x));
end
if all(size(v) == 1)
v = repmat(v, size(x));
end
if all(size(w) == 1)
w = repmat(w, size(x));
end
%make up a set of regions that span the space between the lines
xr = [x(1:end-1); x(1:end-1) + u(1:end-1); x(2:end) + u(2:end); x(2:end)];
yr = [y(1:end-1); y(1:end-1) + v(1:end-1); y(2:end) + v(2:end); y(2:end)];
zr = [z(1:end-1); z(1:end-1) + w(1:end-1); z(2:end) + w(2:end); z(2:end)];
%plot the regions with no edges
h = patch(xr,yr,zr,c, 'LineStyle','none', varargin{:});

Resources