需要帮助在每次迭代时将参数保存到变量

时间:2017-01-09 05:02:56

标签: matlab variables for-loop save iteration

在显示的代码中,我想在一个变量中每次迭代保存一个参数(fval),但不知道如何操作。有人可以建议吗?

clear;
close all;
clc;

for i = 0 : 100
    ii = i * 0.01;
    options = optimset('Display','iter-detailed', ...
                       'Algorithm','interior point', ...
                       'Diagnostics','on');

    options.TolCon = 0;
    options.TolFun = 0;
    [X,faval,exitfag,output,lambda,grad,hessian]=fmincon(@myfun9,0,[],[],[],[],ii,1,@mycon,options);

end;   

1 个答案:

答案 0 :(得分:0)

如果要创建101 separate variables来存储每个值,则不建议这样做。 fval数组faval,用于在每次迭代中存储fval = zeros(101,1); %Pre-allocating memory for fval for k = 0 : 100 ii = k * 0.01; options = optimset('Display','iter-detailed', ... 'Algorithm','interior point', ... 'Diagnostics','on'); options.TolCon = 0; options.TolFun = 0; [X,faval,exitfag,output,lambda,grad,hessian]=fmincon(@myfun9,0,[],[],[],[],ii,1, ... @mycon,options); %Storing value of faval in fval(k+1). Note that indexing starts from 1 in MATLAB fval(k+1) = faval; end 的值,如下所示:

X

顺便说一下,您的代码似乎对所有其他参数的值不感兴趣,即exitfagoutputlambdagradhessi[~,faval]=fmincon(@myfun9,0,[],[],[],[],ii,1, @mycon,options); % exitfag, output, lambda, grad, hessi are skipped automatically as per fmincon doc ,因为这些参数将在每次迭代中被覆盖。如果你对这些价值不感兴趣。您可以使用Pre-allocate跳过存储它们。因此,您可能还想使用以下内容:

# Install Apache

    $ sudo apt-get install apache2

# Install PHP  (PHP7)

    $ sudo apt-get update 
    $ sudo apt-get install php7.0-mysql php7.0-curl php7.0-json php7.0-cgi  php7.0 libapache2-mod-php7

# Install PHP Curl

    sudo apt-get install php-curl

# Install MySQL

    $ sudo apt-get install mysql-server mysql-client
    $ sudo systemctl status mysql

# Manage MySQL Databases (Optional)

    $ sudo apt-get install phpmyadmin

Open terminal, and type:

    $ sudo nano /etc/apache2/apache2.conf

Add the following line at the end:

    include /etc/phpmyadmin/apache.conf

Save and Exit. Restart apache service:

    $ sudo systemctl restart apache2

# Edit config & change html directory

The document root was set to /var/www/html. It was configured in the following file:

    /etc/apache2/sites-available/000-default.conf

So just do:

    $sudo nano /etc/apache2/sites-available/000-default.conf

and change the following line to what you want:

    DocumentRoot /var/www

Also:

    $ sudo gedit /etc/apache2/apache2.conf

Find:

    <Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

Change `/var/www/html` to your preferred directory: `/var/www/`

Restart:

    $ sudo /etc/init.d/apache2 restart

# Enabling mod_rewrite

    $ sudo a2enmod rewrite
    $ service apache2 restart

建议阅读:
1。 tilde (~)
2。 Dynamic Variables
3。 Preallocation
4。 Why does MATLAB have 1 based indexing? 功能