NET combined with MATLAB--least squares linear fitting (C #) __net

Source: Internet
Author: User
Tags int size readline

NET and MATLAB--the least square line fitting (C #) is first A. m file drawgraph.m to make sure it runs in MATLAB.

Here I am the least squares line fitting procedure.

% Least squares linear fitting
%created by Safirst C. Ke 2007.8.29 Wed 14:51

function Drawgraph (coords)
% incoming arguments are two rows of vectors, first behavior x coordinates, and second behavior coordinates.

%axis ([0 100 0 100]);
Grid on;
Hold on;

% shows the position of the point to fit
Plot (coords (1,:), coords (2,:), ' * ');

% Decomposition X,y coordinates
x = coords (1,:)
y = coords (2,:) '

b = Size (coords);
c = Ones (1, B (2));

MT = [C; x];
M = MT ';

%f is a linear function, F = mx + b;
f = INV (MT * M) * MT * y
[' y = ', Num2str (f (2)), ' x + ', Num2str (f (1))]


% shows the final fitted line
x =-max (x): Max (x);
y = f (1) + F (2) * x;


Plot (x, y);
Xlabel (' x axis ');
Ylabel (' Y axis ');
Title (' Least square line fitting by Safirst C. Ke ');

Legend ([' y = ', Num2str (f (2)), ' x + ', Num2str (f (1))]);

The file is then included in the. NET's class library project, and compiles it.

There is a process to understand it, after all. NET cannot compile. m files. How to do it.

By setting the Build event property for this project, add the

Call Plotdemobuild.bat

Then in Plotdemobuild.bat this file to write with MATLAB compiler MCC compiled command line, the most important part is

Mcc-m-silentsetup-vg-b "Dotnet:plotdemocomp,plotter,2.0,private"-D. /.. /src.. /.. /drawgraph.m

In this case, clicking on the build will generate the DLL via MCC, the class library we need.

Then build our real C # project, add references to just the class library, and start writing programs Program.cs

Using System;
Using System.Collections.Generic;
Using System.Text;

Using MathWorks.MATLAB.NET.Utility;

Using MathWorks.MATLAB.NET.Arrays;

These two references are obviously to be added, but fortunately the two namespaces belong to a library MWArray.dll

C:/Program Files/matlab/r2007a/toolbox/dotnetbuilder/bin/win32/v2.0/mwarray.dll

Using Plotdemocomp;

Namespace ConsoleApplication2
{
Class Program
{
[STAThread]
static void Main (string[] args)
{
Try
{
Console.WriteLine ("Please Input the points for your want to fit:");

string[] y = Console.ReadLine (). Trim (). Split ();

int size = Y.length;
double[] x = new Double[size];
for (int i = 0; i < size; i++)
{
X[i] = convert.todouble (Y[i]);
}

double[,] pointvalues = new double[2, SIZE/2];

From the beginning, the next two numbers are one point, so both X and Y are spaced one. such as 1,2,3,4 representative two (1,2), (3,4)

for (int i = 0; i < size; I + + 2)

{
int index = I/2;
Pointvalues[0, index] = x[i];
}
for (int i = 1; i < size; I + + 2)
{
int index = (i-1)/2;
Pointvalues[1, index] = x[i];
}
Plotter plotter = new plotter ();
Plotter.drawgraph ((Mwnumericarray) pointvalues);
Console.ReadLine ();
}
catch (Exception Exception)
{
Console.WriteLine ("Error: {0}", exception);
}
}
}
}

The results of the operation are as follows:

Please Input the points your want to fit:
1 2 3 4 5 6-1-2-3-4-5-6

* Symbol to fit the point, the line is fitted straight line.

Write so much, and then add a curve fitting program.

Special statement: In the next learning. NET C # and Matlab time is not more than 1 months,

Hope the master do not criticize blame, take care of novice learning enthusiasm.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.