12,694,121 members (35,045 online)
alternative version

48.2K views
63 bookmarked
Posted

# Plotting User-Defined R2 in R Functions Using C#

, 15 Jul 2016 GPL3
 Rate this:
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)

## 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.

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);

## 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" %>```

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

## Share

 Engineer Brazil
Senior Analyst

## You may also be interested in...

 Pro Pro

 First Prev Next
 My vote of 5 manoj kumar choubey26-Feb-12 22:44 manoj kumar choubey 26-Feb-12 22:44
 Woa L8N8Hack16-Sep-07 18:30 L8N8Hack 16-Sep-07 18:30
 Neat! Jay Gatsby20-Aug-07 15:56 Jay Gatsby 20-Aug-07 15:56
 Cool [modified] sk8er_boy28723-Jul-07 21:43 sk8er_boy287 23-Jul-07 21:43
 Re: Cool andalmeida24-Jul-07 3:37 andalmeida 24-Jul-07 3:37
 Last Visit: 31-Dec-99 19:00     Last Update: 18-Jan-17 19:11 Refresh 1