Plot ChFunction objects in Matlab (demo_MTLB_functions_plot.cpp)

Entry level demo about how to use Matlab(TM) and the MATLAB module to plot Chrono::Engine objects of the chrono::ChFunction class.

// =============================================================================
// PROJECT CHRONO - http://projectchrono.org
//
// Copyright (c) 2014 projectchrono.org
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file at the top level of the distribution and at
// http://projectchrono.org/license-chrono.txt.
//
// =============================================================================
// Authors: Alessandro Tasora
// =============================================================================
// Demo code for using the ChFunction objects for specifying functions y=f(t)
// and calling Matlab from Chrono (in particular, using Matlab to plot data)
// =============================================================================
#include "chrono/functions/ChFunction.h"
#include "chrono_matlab/ChMatlabEngine.h"
// Use the namespace of Chrono
using namespace chrono;
int main(int argc, char* argv[]) {
std::cout << "Copyright (c) 2017 projectchrono.org\nChrono version: " << CHRONO_VERSION << std::endl;
// Better put the Matlab stuff inside a try{}, since it may throw exception if
// the engine is not started (because Matlab not properly installed)
try {
std::cout << "PERFORM TESTS OF MATLAB<->CHRONOENGINE INTERACTION\n\n";
std::cout << "(please wait few seconds: Matlab engine must be loaded)\n\n";
// This is the object that you can use to access the Matlab engine.
// As soon as created, it loads the Matlab engine (if troubles happen, it
// throws exception).
ChMatlabEngine matlab_engine;
//
// EXAMPLE 1: create a ramp ChFunction, set properties, evaluate it.
//
std::cout << "==== Test 1...\n\n";
f_ramp.SetAngularCoeff(0.1); // set angular coefficient;
f_ramp.SetStartVal(0.4); // set y value for x=0;
// Evaluate y=f(x) function at a given x value, using GetVal() :
double y = f_ramp.GetVal(10);
// Evaluate derivative df(x)/dx at a given x value, using GetDer() :
double ydx = f_ramp.GetDer(10);
std::cout << " ChFunctionRamp at x=0: y=" << y << " dy/dx=" << ydx << "\n\n";
//
// EXAMPLE 2: plot a sine ChFunction
//
std::cout << "==== Test 2...\n\n";
f_sine.SetAmplitude(2); // set amplitude;
f_sine.SetFrequency(0.9); // set frequency;
// Evaluate y=f(x) function along 100 x points:
ChMatrixDynamic<> x_array(100, 1);
ChMatrixDynamic<> y_array(100, 1);
ChMatrixDynamic<> ydx_array(100, 1);
ChMatrixDynamic<> ydxdx_array(100, 1);
for (int i = 0; i < 100; i++) {
double x = (double)i / 50.0;
x_array(i) = x;
y_array(i) = f_sine.GetVal(x);
ydx_array(i) = f_sine.GetDer(x);
ydxdx_array(i) = f_sine.GetDer2(x);
}
// Send resulting vectors of values to Matlab
matlab_engine.PutVariable(x_array, "x_array");
matlab_engine.PutVariable(y_array, "y_array");
matlab_engine.PutVariable(ydx_array, "ydx_array");
matlab_engine.PutVariable(ydxdx_array, "ydxdx_array");
// Plot with Matlab 'plot' command
matlab_engine.Eval("figure; plot(x_array,y_array,'k');");
matlab_engine.Eval("hold on; plot(x_array,ydx_array,'g');");
matlab_engine.Eval("grid on; plot(x_array,ydxdx_array,'r');");
matlab_engine.Eval("legend ('y','dy/dx','ddy/dxdx');");
std::cout << "Press a key to finish... \n";
getchar(); // pause until key..
} catch (std::exception mex) {
std::cerr << mex.what() << std::endl; // Print error on console, if Matlab did not start.
}
return 0;
}
virtual double GetVal(double x) const override
Return the function output for input x.
Definition: ChFunctionSine.cpp:30
void SetStartVal(double y0)
Set the initial value.
Definition: ChFunctionRamp.h:48
virtual double GetDer(double x) const override
Return the first derivative of the function.
Definition: ChFunctionSine.cpp:34
bool PutVariable(ChMatrixConstRef mmatr, std::string varname)
Put a matrix in Matlab environment, specifying its name as variable.
Definition: ChMatlabEngine.cpp:72
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > ChMatrixDynamic
Dense matrix with dynamic size (i.e., with unknown at compile time) and row-major storage.
Definition: ChMatrix.h:75
Ramp function.
Definition: ChFunctionRamp.h:27
Sine function y = A*sin(2*PI*f + phase)`.
Definition: ChFunctionSine.h:28
virtual double GetVal(double x) const override
Return the function output for input x.
Definition: ChFunctionRamp.h:43
Class for accessing the Matlab engine with a C++ wrapper.
Definition: ChMatlabEngine.h:45
Main namespace for the Chrono package.
Definition: ChCamera.cpp:17
virtual double GetDer(double x) const override
Return the first derivative of the function.
Definition: ChFunctionRamp.h:44
void SetAngularCoeff(double ang_coeff)
Set the angular coefficient.
Definition: ChFunctionRamp.h:54
virtual double GetDer2(double x) const override
Return the second derivative of the function.
Definition: ChFunctionSine.cpp:38
bool Eval(std::string mstring)
Evaluate a Matlab instruction (as a string). If error happens while executing, returns false.
Definition: ChMatlabEngine.cpp:63