Adaptive sampling in matlab - matlab

Suppose I have a function which is extremely time consuming to evaluate and I want to generate an interpolated version of it using as few function evaluation as possible. Is there a built in function in Matlab to do that (something like FunctionInterpolation from Mathematica) ?
The procedure is not very difficult and I am aware of freely available implementations (in other languages) like http://scipy-central.org/item/53/1/adaptive-sampling-of-1d-functions but considering that matlab has build in triangular mesh refinement, I think there might be also something like this to be used in one dimension.

You may use fplot with two output arguments, as
[X,Y] = fplot(fun,limits,...)
described in
http://www.mathworks.fr/fr/help/matlab/ref/fplot.html
for instance
fun = #(x) 1./(1+x.^2)
[X,Y] = fplot(fun,[-10, 10])

Related

What are the differences between different gaussian functions in Matlab?

y = gauss(x,s,m)
Y = normpdf(X,mu,sigma)
R = normrnd(mu,sigma)
What are the basic differences between these three functions?
Y = normpdf(X,mu,sigma) is the probability density function for a normal distribution with mean mu and stdev sigma. Use this if you want to know the relative likelihood at a point X.
R = normrnd(mu,sigma) takes random samples from the same distribution as above. So use this function if you want to simulate something based on the normal distribution.
y = gauss(x,s,m) at first glance looks like the exact same function as normpdf(). But there is a slight difference: Its calculation is
Y = EXP(-(X-M).^2./S.^2)./(sqrt(2*pi).*S)
while normpdf() uses
Y = EXP(-(X-M).^2./(2*S.^2))./(sqrt(2*pi).*S)
This means that the integral of gauss() from -inf to inf is 1/sqrt(2). Therefore it isn't a legit PDF and I have no clue where one could use something like this.
For completeness we also have to mention p = normcdf(x,mu,sigma). This is the normal cumulative distribution function. It gives the probability that a value is between -inf and x.
A few more insights to add to Leander good answer:
When comparing between functions it is good to look at their source or toolbox. gauss is not a function written by Mathworks, so it may be redundant to a function that comes with Matlab.
Also, both normpdf and normrnd are part of the Statistics and Machine Learning Toolbox so users without it cannot use them. However, generating random numbers from a normal distribution is quite a common task, so it should be accessible for users that have only the core Matlab. Hence, there is a redundant function to normrnd which is randn that is part of the core Matlab.

Deriving dirac delta function using Matlab symbolic toolbox

i'm new in matlab. I didn't understand how to derive a dirac delta function and then shift it using symbolic toolbox.
syms t
x = dirac(t)
why can't i see the dirac delta function using ezplot(x,[-10,10]) for example?
As others have noted, the Dirac delta function is not a true function, but a generalized function. The help for dirac indicates this:
dirac(X) is not a function in the strict sense, but rather a
distribution with int(dirac(x-a)*f(x),-inf,inf) = f(a) and
diff(heaviside(x),x) = dirac(x).
Strictly speaking, it's impossible for Matlab to plot the Dirac delta function in the normal way because part of it extends to infinity. However, there are numerous workarounds if you want a visualization. A simple one is to use the stem plot function and the > operator to convert the one Inf value to something finite. This produces a unit impulse function (or Kronecker delta):
t = -10:10;
x = dirac(t) > 0;
stem(t,x)
If t and x already exist as symbolic variables/expressions rather than numeric ones you can use subs:
syms t
x = dirac(t);
t2 = -10:10;
x2 = subs(x,t,t2)>0;
stem(t2, x2)
You can write your own plot routine if you want something that looks different. Using ezplot is not likely to work as it doesn't offer as much control.
First, I've not met ezplot before; I had to read up on it. For things that are functionals like your x, it's handy, but you still have to realize it's exactly giving you what it promises: A plot.
If you had the job of plotting the dirac delta function, how would you go about doing it correctly? You can't. You must find a convention of annotating your plot with the info that there is a single, isolated, infinite point in your plot.
Plotting something with a line plot hence is unsuitable for anything but smooth functions (that's a well-defined term). Dirac Delta definitely isn't amongst the class of functions that are smooth. You would typically use a vertical line or something to denote the point where your functional is not 0.

Fast integration technique in matlab?

So I have the following function that I need to code:
Lm = 1/d Integral[exp(-i(a(x)t+mKx)) dx (from 0 to d)
What I have right now is:
L = (1/period) * int(exp(- 1i*(ax*t+(m*K*x))),x,0,period);
subs(L,[t,m],[beta0,tt]);
Where everything is symbolic. This takes a very long time if ax is anything challenging (sin(x)). So I would like to figure out a way to simplify this. I have an array a_x(xi) and I have been referred by colleagues to look into the quad function, but so far I am not sure how to use that.
thanks
If your integrand doesn't change (variables not a function of x) then I see no reason why you couldn't take the output of the symbolic integration and use it numerically without performing the integration:
kmp = K.*m.*period/2
L = exp(-1i*(ax.*t+kmp)).*sin(kmp)./kmp
Otherwise, yes, you should look into Matlab's quadrature integration methods – they work vary similary to sym/int, but are for numerical values and functions. In newer versions of Matab try integral or use quadgk. Something like this:
fun = #(x)exp(-1i*(ax*t+(m*K*x)));
L = (1/period)*integral(fun,0,period);
Note that for highly oscillatory functions, most quadrature methods have difficulty. You should check that your results are actually correct in such cases. If Matlab's built-in quadrature routines have trouble, you could look into Levin integration schemes or maybe this.

matlab genetic algorithm solver complex input and output

My Matlab program has multiple inputs as a struct (in.a, in.b, etc.)
and multiple outputs (out.a, out.b, etc.)
I would like to use the genetic algorithm solver from teh optimization toolbox to find the best input in.a, while all the other inputs are constant. The fitness is one of the outputs, e.g. out.b(2,3).
How do I "tell" the solver this?
Thanks
Daniel
It is not uncommon in programming to have a situation where what is most convenient for your function and what some library call expects of it don't agree. The normal resolution to such a problem is to write a small layer in between that allows the two to talk; an interface.
From help ga:
X = GA(FITNESSFCN,NVARS) finds a local unconstrained minimum X to the
FITNESSFCN using GA. [...] FITNESSFCN accepts a vector X of size
1-by-NVARS, and returns a scalar evaluated at X.
So, ga expects vector input, scalar output, whereas you have a structure going in and out. You would have to write the following (sub)function:
function Y = wrapper_Objfun(X, in)
in.a = X; %# variable being optimized
out = YOUR_REAL_FUNCTION(in); %# call to your actual function
Y = out.b(2,3); %# objective value
end
and then the call to ga will look like
X = ga(#(x) wrapper_Objfun(x,in), N);
where N is however large in.a should be.
Also have a read about it in Matlab's own documentation on the subject.

How to draw the transfer function of an RC circuit using MATLAB?

If I have an RC circuit with transfer function 1/(1+sRC) how do I draw the transfer function using MATLAB?
Num2=[1];
Den2=[R*C 1];
RCcirc=tf(Num2,Den2);
How do I declare the R and the C so that there are no errors?
tf is the wrong tool for plotting the transfer function. Try these instead:
Use linspace to generate a range of values for s. Give R and C reasonable values of your choice.
Read up on arithmetic operations in MATLAB, especially ./
Look at how to use plot and familiarize yourself with the command using some simple examples from the docs.
With these you should be able to plot the transfer function in MATLAB :)
First of all you need to understand what transfer function you want. Without defined values of R and C you won't get any transfer function. Compare it to this, you want to plot a sine wave: x = sin(w*t), I hope you can agree with me that you cannot plot such a function (including axes) unless I specifically say e.g. t is the time, ranging from 0 seconds to 10 seconds and w is a pulsation of 1 rad/s. It's exactly the same with your RC network: without any values, it is impossible for numerical software such as MATLAB to come up with a plot.
If you fill in those values, you can use th tf function to display the transfer function in whatever way you like (e.g. a bode plot).
On the other hand, if you just want the expression 1/(1+s*R*C), take a look at the symbolic toolbox, you can do such things there. But to make a plot, you will still have to fill in the R and C value (and even a value for your Laplace variable in this case).