Skip to content

Function Surface

1
fsurf(fn);

example_fsurf_1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;

    fsurf([](double x, double y) { return sin(x) + cos(y); });

    show();
    return 0;
}

More examples

example_fsurf_2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;

    fsurf([](double x, double y) { return erf(x) + cos(y); }, {-5, 0, -5, 5});
    hold(on);
    fsurf([](double x, double y) { return sin(x) + cos(y); }, {0, 5}, {-5, 5});
    hold(off);

    show();
    return 0;
}

example_fsurf_3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;

    auto r = [](double u, double v) { return 2 + sin(7 * u + 5 * v); };
    auto funx = [r](double u, double v) { return r(u, v) * cos(u) * sin(v); };
    auto funy = [r](double u, double v) { return r(u, v) * sin(u) * sin(v); };
    auto funz = [r](double u, double v) { return r(u, v) * cos(v); };
    fsurf(funx, funy, funz, {0, 2 * pi, 0, pi})->lighting(on);

    show();
    return 0;
}

example_fsurf_4

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;

    auto fn = [](double x, double y) { return y * sin(x) - x * cos(y); };
    fsurf(fn, {-2 * pi, 2 * pi});
    title("ysin(x) - xcos(y) for x and y in [-2π,2π]");
    xlabel("x");
    ylabel("y");
    zlabel("z");
    box(on);

    auto ax = gca();
    ax->xticks(iota(-2 * pi, pi / 2, 2 * pi));
    ax->xticklabels(
        {"-2π", "-3π/2", "-π", "-π/2", "0", "π/2", "π", "3π/2", "2π"});

    ax->yticks(iota(-2 * pi, pi / 2, 2 * pi));
    ax->yticklabels(
        {"-2π", "-3π/2", "-π", "-π/2", "0", "π/2", "π", "3π/2", "2π"});

    show();
    return 0;
}

example_fsurf_5

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;

    auto funx = [](double u, double v) { return u * sin(v); };
    auto funy = [](double u, double v) { return -u * cos(v); };
    auto funz = [](double, double v) { return v; };
    fsurf(funx, funy, funz, {-5, 5, -5, -2}, "--")->edge_color("g");
    hold(on);

    fsurf(funx, funy, funz, {-5, 5, -2, +2})->edge_color("none");
    hold(off);

    show();
    return 0;
}

example_fsurf_6

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;

    auto funx = [](double u, double v) {
        return exp(-std::abs(u) / 10.) * sin(5 * std::abs(v));
    };
    auto funy = [](double u, double v) {
        return exp(-std::abs(u) / 10.) * cos(5 * std::abs(v));
    };
    auto funz = [](double u, double) { return u; };
    fsurf(funx, funy, funz, std::array<double, 2>{-30., +30.})->face_alpha(.5);

    show();
    return 0;
}

example_fsurf_7

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;

    fsurf([](double x, double y) { return peaks(x, y); }, {-3, +3})
        ->contour_base(true);

    show();
    return 0;
}

example_fsurf_8

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;

    auto funx = [](double s, double) { return sin(s); };
    auto funy = [](double s, double) { return cos(s); };
    auto funz = [](double s, double t) { return t / 10 * sin(1. / s); };

    tiledlayout(2, 1);
    nexttile();
    fsurf(funx, funy, funz, std::array<double, 2>{-5, +5}, "", 20);
    view(-172, 25);
    title("Decreased Mesh Density");

    nexttile();
    fsurf(funx, funy, funz, std::array<double, 2>{-5, +5}, "", 80);
    view(-172, 25);
    title("Increased Mesh Density");

    show();
    return 0;
}