C# call the Matlab function to compute the nonlinear programming problem?

I just know how to solve a linear programming,but I didn't know how to abou a nonlinear programming. How can I call the Matlab function in the WinForms to solve a nonlinear programming? That means C# pass the required data to Matlab function ,and I didn't know how to pass parameters between C# and Matlab as to solve anonlinear programming. Would anyone give me some examples? Thanks a lot!

Answers (1)

One option is to start MATLAB up as a COM server from your C# application. The example Calling MATLAB Software from a C# Client describes how you can pass data in and out of MATLAB.

4 Comments

Thanks for your reply. I tried the two mehods,.NET component and COM component,but it can't work.My qestion solve a nonlinear programming question
which need pass data between C# and Matlab. There are sveral queations puzzled me:
1. C# have read several data form database and must pass these data to Matlab in order to call Matlab's function to compute it;
2. Matlab must pass the result back to C# when Matlab have finished it.
3. When C# get the result ,and then display it to users.
However,I didn't how to pass data form C# to Matlab,and then pick result up from Matlab.
Suppose I have a nonlinear programming question like this:
min f=-30*x1-450*x2
s.t. 0.5*x1+2*x2+0.25*x2^2<=800
x1>=0,x2>=0
Could you helpme to solve it? Thanks a lot.
You can create a MATLAB script/function to solve the equation (I would recommend first testing your function with some sample data before you try invoking it from .NET). Once you have that ready, you can follow the "Calling MATLAB Software from a C# Client" example to pass in variables required by your function. Additionally, you would use matlab.execute("command to run your script") to run your file.
Thanks for your reply.There are several warnings in the output when I use the 'fmincon' function to compute a non-linear programming problem.If I use the matlab.execute("command") function,the result would contain the warning or error messages.How can I solve it?
Furthermore,my project would need C# pass 10 thousands data to Matlab'fmincon function to get the optimized result,and then return the result to client window.How can I achieve it?
Additionally,for the objective function and non-linear constrains,how can I pass the decision variables' coefficients to the fmincon function.Thank you very much!
I don't have much experience with non-linear programming - please create another question for that part, so someone else can help you with it. To disable all warnings, you can use "warning off" at the start of your function - i would recommend using "warning on" at the end of the function, to restore the original warning state. You should make your code error-free to be able to successfully obtain results in C#.
Regarding passing data - again, you can simply use PutFullMatrix and GetFullMatrix as described in the cited example. Another alternative that I didn't mention before is MATLAB Builder NE (http://www.mathworks.com/products/netbuilder/), but you may not need it unless you want to run your C# application that does not have MATLAB installed.

Sign in to comment.

Asked:

on 29 Mar 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!