matlab程序运行慢

利用Matlab编写函数求按照指数形式傅里叶展开系数式,重复调用时程序运行缓慢,怎么解决?


```c
function out = FourierUnfolds(f, expand)
    TimeVariable = sym('TimeVariable');
    expandAll = 2 * expand + 1;
    NewMatrix(:, expandAll) = 0;
    number = 1; 
    %按照指数函数傅里叶展开
    for Factor = -expand : 1 : expand
        if number <= expandAll
            C = int(matlabFunction(f)*exp(-1i*Factor*TimeVariable),0,2*pi)/2/pi;
            NewMatrix(1, number) =  C;
            number = number + 1;
        end 
    end
    out = NewMatrix;
end


```

  • 你可以看下这个问题的回答https://ask.csdn.net/questions/7724281
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:matlab在循环时如何跳过几个数,matlab如何得到一个数组的行数和列数, matlab判断数组的长度
  • 你还可以看下matlab参考手册中的 matlab 编辑或创建文件 edit
  • 除此之外, 这篇博客: MATLAB强化学习工具箱(五)使用自定义函数创建MATLAB环境中的 使用函数名创建环境 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 要定义自定义环境,首先指定自定义步骤和重置函数。这些函数必须在当前工作文件夹中或在MATLAB路径中。

    自定义reset函数设置环境的默认状态。此函数必须具有以下签名。

     [InitialObservation,LoggedSignals] = myResetFunction()
    

    此时出现
    在这里插入图片描述
    点击箭头,进入示例文件夹,或自行创建.m文件
    在这里插入图片描述
    要将信息从一个步骤传递到下一个步骤,例如环境状态,请使用LoggedSignals。对于本例,LoggedSignals包含车-杆环境的状态:小车的位置和速度、摆角和摆角导数。reset函数在每次重置环境时将cart角度设置为一个随机值。

    对于本例,使用在myResetFunction.m中定义的自定义重置函数。

    type myResetFunction.m
    

    出现以下内容

    function [InitialObservation, LoggedSignal] = myResetFunction()
    % Reset function to place custom cart-pole environment into a random
    % initial state.
    % Theta (randomize)
    T0 = 2 * 0.05 * rand() - 0.05;
    % Thetadot
    Td0 = 0;
    % X
    X0 = 0;
    % Xdot
    Xd0 = 0;
    % Return initial environment state variables as logged signals.
    LoggedSignal.State = [X0;Xd0;T0;Td0];
    InitialObservation = LoggedSignal.State;
    end

    自定义step函数指定环境如何根据给定的操作推进到下一个状态。此函数必须具有以下签名。

    [Observation,Reward,IsDone,LoggedSignals] = myStepFunction(Action,LoggedSignals)
    

    为了获得新的状态,环境将动态方程应用到存储在LoggedSignals中的当前状态,这类似于给微分方程一个初始条件。新的状态存储在LoggedSignals中,并作为输出返回。

    对于本例,使用在myStepFunction.m中定义的自定义步骤函数。为了简化实现,此函数重新定义了物理常量,例如cart质量,每个时间步都执行。

    type myStepFunction.m
    

    出现以下内容

    function [NextObs,Reward,IsDone,LoggedSignals] = myStepFunction(Action,LoggedSignals)
    % Custom step function to construct cart-pole environment for the function
    % name case.
    %
    % This function applies the given action to the environment and evaluates
    % the system dynamics for one simulation step.
    % Define the environment constants.
    % Acceleration due to gravity in m/s^2
    Gravity = 9.8;
    % Mass of the cart
    CartMass = 1.0;
    % Mass of the pole
    PoleMass = 0.1;
    % Half the length of the pole
    HalfPoleLength = 0.5;
    % Max force the input can apply
    MaxForce = 10;
    % Sample time
    Ts = 0.02;
    % Pole angle at which to fail the episode
    AngleThreshold = 12 * pi/180;
    % Cart distance at which to fail the episode
    DisplacementThreshold = 2.4;
    % Reward each time step the cart-pole is balanced
    RewardForNotFalling = 1;
    % Penalty when the cart-pole fails to balance
    PenaltyForFalling = -10;
    % Check if the given action is valid.
    if ~ismember(Action,[-MaxForce MaxForce])
    error(‘Action must be %g for going left and %g for going right.’,…
    -MaxForce,MaxForce);
    end
    Force = Action;
    % Unpack the state vector from the logged signals.
    State = LoggedSignals.State;
    XDot = State(2);
    Theta = State(3);
    ThetaDot = State(4);
    % Cache to avoid recomputation.
    CosTheta = cos(Theta);
    SinTheta = sin(Theta);
    SystemMass = CartMass + PoleMass;
    temp = (Force + PoleMassHalfPoleLengthThetaDotThetaDotSinTheta)/SystemMass;
    % Apply motion equations.
    ThetaDotDot = (GravitySinTheta - CosThetatemp) / …
    (HalfPoleLength*(4.0/3.0 - PoleMassCosThetaCosTheta/SystemMass));
    XDotDot = temp - PoleMassHalfPoleLengthThetaDotDotCosTheta/SystemMass;
    % Perform Euler integration.
    LoggedSignals.State = State + Ts.
    [XDot;XDotDot;ThetaDot;ThetaDotDot];
    % Transform state to observation.
    NextObs = LoggedSignals.State;
    % Check terminal condition.
    X = NextObs(1);
    Theta = NextObs(3);
    IsDone = abs(X) > DisplacementThreshold || abs(Theta) > AngleThreshold;
    % Get reward.
    if ~IsDone
    Reward = RewardForNotFalling;
    else
    Reward = PenaltyForFalling;
    end
    end

    使用定义的观察规范,操作规范和函数名称构造定制环境。

    env = rlFunctionEnv(ObservationInfo,ActionInfo,'myStepFunction','myResetFunction');
    

    要验证环境的操作,rlFunctionEnv会在创建环境后自动调用validateEnvironment。

  • 您还可以看一下 硬核野生技术咨询客服小李老师的matlab零基础入门路径规划城市遍历机器人路径等问题课程中的 数据类型小节, 巩固相关知识点