Click here to Skip to main content
12,694,121 members (35,045 online)
Click here to Skip to main content
Add your own
alternative version

Stats

48.2K views
491 downloads
63 bookmarked
Posted

Plotting User-Defined R2 in R Functions Using C#

, 15 Jul 2016 GPL3
Rate this:
Please Sign up or sign in to vote.
Plotting user defined R2 in R functions using C#

Introduction

This article describes very briefly (and on a basic level) how to plot user interactive 3D R2 in R surfaces using C# ASP.NET. The example enables the user to interact with the drawing in 5 aspects as below:

  • function expression z = f(x,y)
  • domain
  • integration step
  • rotation about x, y and z axis
  • model (wireframe or fill)

Screenshot - main.png

Background

  • A point in R3 is a set of 3 float values (x,y,z) which defines a position.
  • R2 in R functions are expresions in the form z = f(x,y) where x and y are a subset of the function domain.
  • The R2 in R domain is the R2 or better, the xy plane.

X,Y,Z Axis and a subdomain in R2
The cartesian coordinates

The objective is to get a set of z coordinates applying a generic function z=f(x,y) to a discrete domain, or better a matrix of defined x and y points as the image grid above.

How to Perform This

At first we need to define the domain. Let's choose [-1.7,1.3] for x and [-2.1,2.5] for y what means x will run from -1.7 to 1.3 and y will run from -2.1 to 2.5.

How to Run the Points

As we are working with R coordinates we have infinite points between 2 coordinates, so for this we need a step variable, which means the integration we will use to get the grid points.

Let's define our step as 0.1, and now we can show some code:

double x0=-1.7, x1=1.3, y0=-2.1, y1=2.5;

double step = 0.1;
double x=x0;
while(x<=x1)
     {
     //...
     y=y0;

     while(y<=y1)
          {
          //...
          y+=step;
          }
     x+=step;
     }

Now with above definitions we can handle a generation of z coordinates from running the xy discrete subdomain by using the generic function z = f(x,y)

double x0=-1.7, x1=1.3, y0=-2.1, y1=2.5;

double step = 0.1;
double x=x0;
double z;

while(x<=x1)
     {
     //...
     y=y0;

     while(y<=y1)
          {
          //...
          y+=step;
          z = function(x,y); // at this point we have x,y and z coordinates
          // all we need now is to keep this information in a data structure
          // to plot the whole data in a next step
          }
     x+=step;
     }

Now the Tricky Part

We could use a MathExpressionParser with the expression string, but here I made it more simple with the help of a template with a tag. The process is easy: read the template file, replace the tag, and write the ASPX file which shows the image.

string contents = ReadFile(Server.MapPath("./dynafuncapp.txt"));
contents = contents.Replace("@EXPRESSION", functionStr);
SaveFile(Server.MapPath("./dynacontent.aspx"), contents);
// note you must have write access to the folder ***

Putting Everything Together

The Template File

<%@ Page Language="c#" Debug="true" Explicit="True" %>
<%@ Import Namespace="System.Data" %>

The Main File

<%@ Page Language="c#" Debug="true" Explicit="True" %>
<%@ Import Namespace="System.Data" %>

sin(pow(x,3)-7*x*y-pow(y,4))
Sample of generated function
sin(pow(x,3)-7*x*y-pow(y,4))

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

andalmeida
Engineer
Brazil Brazil
Senior Analyst

Linkedin Profile

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 5 Pin
manoj kumar choubey26-Feb-12 22:44
membermanoj kumar choubey26-Feb-12 22:44 
GeneralWoa Pin
L8N8Hack16-Sep-07 18:30
memberL8N8Hack16-Sep-07 18:30 
GeneralNeat! Pin
Jay Gatsby20-Aug-07 15:56
memberJay Gatsby20-Aug-07 15:56 
GeneralCool [modified] Pin
sk8er_boy28723-Jul-07 21:43
membersk8er_boy28723-Jul-07 21:43 
AnswerRe: Cool Pin
andalmeida24-Jul-07 3:37
memberandalmeida24-Jul-07 3:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 15 Jul 2016
Article Copyright 2007 by andalmeida
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid