从MATLAB代码生成独立c语言代码

更新时间:2023-07-25 06:26:58 阅读: 评论:0

Matlab 高斯拟合help lsqcurvefit
 MATLAB Coder可以从MATLAB代码生成独立的、可读性强、可移植的C/C++代码。
使用MATLAB Coder产生代码的3个步骤:准备用于产生代码的MATLAB算法;检查MATLAB代码的兼容性(有些matlab代码语句并不能生成c/c++代码);产生最终使用的源代码或MEX
利用MATLAB Coder生成c++代码,并在vs2008中验证:
一个简单的例子,两数相乘
1、安装matlab2011a或者更新版本;
2、简单生成一个foo.m文件;
function c = foo(a, b)%#codegen
%This function muliplies a and b
c = a * b
其中,%#codegen可以防止出现警告错误
3、在命令窗口,输入mex -tpu,选中一个存在的编译器;
4、在命令窗口输入coder(图形界面),回车,弹出MATLAB Coder Project对话框;
5、在New选项卡Name中输入一个工程名foo.prj;点击Ok,弹出MATLAB Coder MEX Function对话框;
6、在Overview选项卡中,点击Add files,弹出对话框,选中foo.m打开;
7、单击变量a,选择Define by Example…,弹出MATLAB Coder Define by Example对话框,在MATLAB Expression中输入5,点击OK;同样变量b也进行相应操作,输入6
8、选中Build选项卡,Output type中选择c/c++ Static Library;选中Generate code only
9、点击More ttingsGeneralàLanguage选择C++Interface选项中去掉所有选项;Clo
10、点击Build,进行编译;点击View report不可救药造句,弹出Code Generation Report对话框,此时,变量abc会显示相应的变量信息;
11、利用vs2008建立一个控制台应用程序,将生成的相关文件试用期不交社保foo.hfoo.cpprtwtypes.hfoo_types.h拷到相关目录下并添加到应用程序中;
12、在foo.cpp珠海市天气预报文件中添加#include “stdafx.h”
13test.cpp文件中代码为:
#include "stdafx.h"
#include "foo.h"
#include <iostream>
 
using namespace std;
 
int _tmain(int argc, _TCHAR* argv[])
{
 
    double a = 0.0, b = 0.0, c = 0.0;恭维人
 
    cin>>a>>b;
 
    c = foo(a, b);
 
    cout<<"c = "<<c<<endl;
 软件推广
    return 0;
}
8个字的成语
 成语之最
一个复杂的例子,求一个数的n次方根
1  两个.m文件:
nrt.m:
function [nth_rt, iterations, hstry] = nrt(varargin)%#codegen
%This function will u a Newton Search Technique to find
%the nth root of a number, a, to the tolerance, tol.
%The square root
% nrt(10, 2), or nrt(10, 2, 1e-9)
%The "n" root
%nrt(10, n), or nrt(10, n, 1e-9)
 
a = varargin{1};
n = varargin{2};
 
if nargin ~= 3
    tol = 1e-9;
el
大疆遥控器    tol = varargin{3};
end
 
if a < 0
    nth_rt = 0;
    iterations = 0;
    hstry = 0;
el
    [nth_rt, hstry] = newtonSearchAlgorithm(a, n, tol);
    iterations = length(find(hstry ~= 0));
    %iterations = sum(hstry ~= 0);
end
 
newtonSearchAlgorithm.m
function [x, h] = newtonSearchAlgorithm(b, n, tol) %#codegen
%Given, "a", this function finds the nth root of a
%number by finding where: x^n-a = 0
coder.inline('never'); %使其生成一个单独的c++文件
notDone = 1;
aNew    = 0; %Refined Guess Initialization
a      = 1; %Initial Guess
cnt    = 0;
h = zeros(50, 1);
h(1)    = a;
while notDone
    cnt = cnt + 1;
    [curVal, slope] = f_and_df(a, b, n); %  square
    yint = curVal - slope * a;
    aNew = -yint / slope; %The new guess
    h(cnt) = aNew;
    if (abs(aNew-a) < tol) %Break if it's converged
        notDone = 0;
    elif cnt > 49 %after 50 iterations, stop
        notDone = 0;
        aNew = 0;

本文发布于:2023-07-25 06:26:58,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1095711.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:输入   代码   相应   产生
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图