Octave - logsig transfer function - matlab

Does Octave have a built-in logistic transfer function similar to Matlab's logsig function?

I don't believe Octave does, but you can certainly create logsig outputs yourself. The logsig transfer function (or the Log-Sigmoid function... or simply the Sigmoid function) is simply defined as:
a = 1 ./ (1 + exp(-n));
n would be the input values stored in a vector / matrix / etc. As such, simply place your values into a matrix / vector into n, then use the above code to apply the logsig function to every value that is defined in n.
Example
n = [0; 1; -0.5; 0.5];
a = 1 ./ (1 + exp(-n))
a =
0.5000
0.7311
0.3775
0.6225
Comparing this with MATLAB's logsig function, we get:
a2 = logsig(n)
a2 =
0.5000
0.7311
0.3775
0.6225

logsig is part of the nnet octave-forge package. http://sourceforge.net/p/octave/code/HEAD/tree/trunk/octave-forge/main/nnet/inst/logsig.m
If it's not in core Matlab (Neural Network Toolbox in this case) you should have a look at the corresponding octave-forge package. Unfortunally nnet is unmaintained.
The logsig.m linked is basically the same as rayrengs but also checks for finite.

Related

Is it possible to find a block for multi-input single-output transfer function in Matlab simulink?

I have a system with two inputs and one common output.
Let inputs be in1, in2 and output - out.
So I have two transfer functions: out / in1, out / in2.
Using simulink I can use transfer fcn block for each transfer function and then sum their outputs to get a desired output.
But is it possible to join transfer functions out/in1, out/in2 somehow together and to use some simulink block to avoid summation of transfers functions outputs?
Thank you for your time and help in advance!
% in symbolic
syms Ht s D K Hg
TF1 = tf([D K],[4*Hg*Ht (2*Hg*D+2*Ht*D) (2*Hg*K+2*Ht*K) 0]);
TF2 = tf([-2*Ht -D -K],[4*Hg*Ht (2*Hg*D+2*Ht*D) (2*Hg*K+2*Ht*K) 0]);
% or in numerical way
Ht = 2.2667;
Hg = 0.92952;
D = 2.29;
K = 1.0216;
TF1 = tf([D K],[4*Hg*Ht (2*Hg*D+2*Ht*D) (2*Hg*K+2*Ht*K) 0]);
TF2 = tf([-2*Ht -D -K],[4*Hg*Ht (2*Hg*D+2*Ht*D) (2*Hg*K+2*Ht*K) 0]);
There is a really simple solution for this. Given two transfer functions TF1=tf(num1,den1) and TF2=tf(num2,den2), the sum TF1+TF2 can be expressed as a single transfer function tf(num1*den2+num2*den1,den1*den2).
For actual implementation, you will want to use conv to compute the numerator and denominator polynomials from the polynomial coefficient vectors from the component transfer functions.
num = polyadd(conv(num1,den2),conv(num2,den1));
den = conv(den1,den2);
Note polyadd is not a built-in Matlab function but you can write your own or use https://stackoverflow.com/a/55085308.
If you already have the single-input, single-output (SISO) transfer function objects tf1 and tf2 in Matlab, you can also get the sum using tf1+tf2 or by using parallel(tf1,tf2,1,1,1,1) (see https://www.mathworks.com/help/control/ref/parallel.html).
Why does this work?
You have two transfer functions:
and you are interested in the combined system:
which has the the transfer function:

Find z-transform and plot it's pole-zero map with MATLAB

I have this function:
x[n] = (1/2) ^ n * u[n] + (-1/3) ^ n * u[n]
I need to do two things with this using MATLAB:
Find it's z-transform.
Plot it's poles and zeros.
I am using the following code:
syms n;
f = (1/2)^n + (-1/3)^n;
F = ztrans(f);
I get the z-transform in the F variable, but I can't see how to create it's pole-zero plot. I am using the built-in function pzmap (pzmap(F);), but it doesn't seem to work with the output of ztrans(f).
What am I doing wrong? Do I need to change the z-transform into some other form like like a transfer function model or a zero-pole gain model? If so, can someone explain how that can be done using the output of ztrans(f)?
The first bit of code you gave uses symbolic math to solve for the z-transform. You'll need to convert the output to a discrete-time model supported by the Control System toolbox.
syms n;
f = (1/2)^n + (-1/3)^n;
F = ztrans(f)
returns z/(z - 1/2) + z/(z + 1/3). You can optionally use collect to convert this
F2 = collect(F)
to (12*z^2 - z)/(6*z^2 - z - 1). Then you'll want to find the coefficients of the polynomials in the numerator and denominator and create a discrete-time transfer function object with tf for a particular sampling period:
[num,den] = numden(F2);
Ts = 0.1; % Sampling period
H = tf(sym2poly(num),sym2poly(den),Ts)
Then pzmap(H) will produce a plot like this:

optimize the function in MATLAB

I have tried many times taking different initial values of c. I did not get optimum values of c using function fminsearch. I have found more error between simulated and measured values of sigma. Please help me: how can optimize my function?
clc
close all
clear all
syms c sigma est_bio mea_bio
sigma=[-15.1015 -13.7879 -13.0576 -12.7818 -12.3839 -11.7587 -11.1756 -10.6291 -9.9176];
mea_bio=[0.181 0.204 0.529 0.632 1.059 1.533 1.934 1.977 1.861];
%%% create model function q with parameters
q = #(c, mea_bio) ((c(1)/(-2*c(2))).*(1-exp(2*c(2).*mea_bio))+c(3).*exp(2*c(2).*mea_bio))
%// create the desired error-functions for minimization
h = #(c) sum((q(c, mea_bio) - sigma).^2); %// default minimizaton function
c= [-.05 -.0500 -.0500]; % an initial guess
[p_fit_e, r_e] = fminsearch(h, c) % Optimize
est_q = ((c(1)/(-2*c(2))).*(1-exp(2*c(2).*mea_bio))+c(3).*exp(2*c(2).*mea_bio))
err=est_q-sigma
Why do you define c, sigma est_bio and mea_bio as symbolic variables? It doesn't make sense. This is a numerical optimization, no need to involve symbolic computations. Remove that line from your code and it should work:
syms c sigma est_bio mea_bio

Solving an ODE when the function is given as discrete values -matlab-

I have the following ODE:
x_dot = 3*x.^0.5-2*x.^1.5 % (Equation 1)
I am using ode45 to solve it. My solution is given as a vector of dim(k x 1) (usually k = 41, which is given by the tspan).
On the other hand, I have made a model that approximates the model from (1), but in order to compare how accurate this second model is, I want to solve it (solve the second ODE) by means of ode45. My problem is that this second ode is given discrete:
x_dot = f(x) % (Equation 2)
f is discrete and not a continuous function like in (1). The values I have for f are:
0.5644
0.6473
0.7258
0.7999
0.8697
0.9353
0.9967
1.0540
1.1072
1.1564
1.2016
1.2429
1.2803
1.3138
1.3435
1.3695
1.3917
1.4102
1.4250
1.4362
1.4438
1.4477
1.4482
1.4450
1.4384
1.4283
1.4147
1.3977
1.3773
1.3535
1.3263
1.2957
1.2618
1.2246
1.1841
1.1403
1.0932
1.0429
0.9893
0.9325
0.8725
What I want now is to solve this second ode using ode45. Hopefully I will get a solution very similar that the one from (1). How can I solve a discrete ode applying ode45? Is it possible to use ode45? Otherwise I can use Runge-Kutta but I want to be fair comparing the two methods, which means that I have to solve them by the same way.
You can use interp1 to create an interpolated lookup table function:
fx = [0.5644 0.6473 0.7258 0.7999 0.8697 0.9353 0.9967 1.0540 1.1072 1.1564 ...
1.2016 1.2429 1.2803 1.3138 1.3435 1.3695 1.3917 1.4102 1.4250 1.4362 ...
1.4438 1.4477 1.4482 1.4450 1.4384 1.4283 1.4147 1.3977 1.3773 1.3535 ...
1.3263 1.2957 1.2618 1.2246 1.1841 1.1403 1.0932 1.0429 0.9893 0.9325 0.8725];
x = 0:0.25:10
f = #(xq)interp1(x,fx,xq);
Then you should be able to use ode45 as normal:
tspan = [0 1];
x0 = 2;
xout = ode45(#(t,x)f(x),tspan,x0);
Note that you did not specify what values of of x your function (fx here) is evaluated over so I chose zero to ten. You'll also not want to use the copy-and-pasted values from the command window of course because they only have four decimal places of accuracy. Also, note that because ode45 required the inputs t and then x, I created a separate anonymous function using f, but f can created with an unused t input if desired.

Calculating Covariance Matrix in Matlab

I am implementing a PCA algorithm in MATLAB. I see two different approaches to calculating the covariance matrix:
C = sampleMat.' * sampleMat ./ nSamples;
and
C = cov(data);
What is the difference between these two methods?
PS 1: When I use cov(data) is that unnecessary:
meanSample = mean(data,1);
data = data - repmat(data, nSamples, 1);
PS 2:
At first approach should I use nSamples or nSamples - 1?
In short: cov mainly just adds convenience to the bare formula.
If you type
edit cov
You'll see a lot of stuff, with these lines all the way at the bottom:
xc = bsxfun(#minus,x,sum(x,1)/m); % Remove mean
if flag
xy = (xc' * xc) / m;
else
xy = (xc' * xc) / (m-1); % DEFAULT
end
which is essentially the same as your first line, save for the subtraction of the column-means.
Read the wiki on sample covariances to see why there is a minus-one in the default path.
Note however that your first line uses normal transpose (.'), whereas the cov-version uses conjugate-transpose ('). This will make the output of cov different in the context of complex-valued data.
Also note that cov is a function call to a non-built in function. That means that there will be a (possibly severe) performance penalty when using cov in a loop; Matlab's JIT compiler cannot accelerate non-built in functions.