matlab function which is a function of an intergral - function

I need to write my own function which has the form f(x,y)=Integrate(g(x,y,z),z from 0 to inf). so the code I used was:
function y=f(x,y)
g=#(z)exp(-z.^2)./(z.^x).*(z.^2+y.^2).^(x/2);% as a function of x,y and z
y=quadgk(g,0,inf)
and if I call it for a single value like f(x0,y0), it works but if I try to calculate something like f([1:10],y0), then the error message says that there is something wrong with the times and dimension. In principle I can use for loops but then my code slows down and takes forever. Is there any help I can get from you guys? or references?
I'm trying to avoid the for loop since in matlab it's much faster to use matrix computation than to use for loop. I wonder if there is any trick that I can take advantage of this feature.
Thanks for any help in advance,
Lynn

Perhaps you can try to transpose the interval, creating row based values instead of column based f([1:10]',y0). Otherwise something in your function might be wrong, for example to get x^y to work with lists as input, you have to prefix with a dot x.^y. The same for mulitply and division I think..

If loop is no problem for you, you should do something like:
function y2=f(x,y)
y2=zeros(size(x));
for n=1:numel(x)
g=#(z)exp(-z.^2)./(z.^x(n)).*(z.^2+y.^2).^(x(n)/2);% as a function of x,y and z
y2(n)=quadgk(g,0,inf)
end

The problem here is that quadk itself uses vectors as argument for g. Then you have in g somethink like z.^x, which is the power of two vectors that is only defined if z and x have the same dimension. But this is not what you want.
I assume that you want to evaluate the function for all arguments in x and that the output vector has the same dimension as x. But this does not seem to be possible since even this simple example
g=#(x)[x;x.^2]
quad(g,0,1)
does not work:
Error using quad (line 79)
The integrand function must return an output vector of the same length as the
input vector.
A similar error shows when using quadgk. The documentation also says that this routine works only for scalar functions and this is not surprising since an adaptive quadrature rule would in general use different points for each function to evaluate the integral.
You have to use quadvinstead, which can integrate vector valued functions. But this gives wrong results since your function is integrated in the interval [0,\infty).

Related

Octave: How to solve a one order differential equation with variable coefficients

I'm trying to solve a one order differential equation with variable coefficients of the following form:
xdot(1)=a(t)*x(1)+b;
where b=a constant and where a(t) = a time dependent function. I know that I can solve this equation by hand butt a(t) is a quite complex function.
So, my problem is the following. a(t) is a function which I know its values from an experiment (I've got all the results in a file) --> a(t) is a vector (n x 1) which is a problem because x(1)= xdot(1)=a scalar. So, how could I solve this equation, with lsode ?
Possibly I have underestimated your problem, but the way I read it, you are asking to integrate a first order ODE. In general there are two ways to proceed,, implicit methods and explicit methods. Here is the crudest, but easiest to understand, method I can come up with:
nt=101;a=-ones(1,nt);b=1/2;x=NaN*ones(1,nt);x(1)=pi;dt=0.01;
for kt=2:nt
dxdt=a(kt-1)*x(kt-1)+b;
x(kt)=x(kt-1)+dxdt*dt;
endfor
plot(x)
I have assumed a<0 so there is no tendency to blow up. You will want to set it equal to your observed values.

Writing a simple user-defined function in Maple

I'm a total novice in Maple (I usually use R) but I'm trying to do some analytic transformations on a user-defined function, hence my choice of Maple.
The function is in t and I have a vector of known values (basically, known constants) (t_1,..., t_n) which is passed to the function and I also know the values for alpha, beta and miu.
I cannot figure out how to code the summation of exponentials in Maple: in R I would simply use sum().
I have looked at the examples in http://www.maplesoft.com/support/help/maple/view.aspx?path=sum but none seems to apply to my case. I don't want to evaluate the function but I want to be able to find, analytically, the integral or the derivative of it (or of functions of it.)
Links to manuals or web-pages that deal with similar problems/cases are very much welcome!
Make the vector of constants global, not passed in. I'll call it T. Then the function is
lambda:= t-> mu+alpha*add(exp(-beta*(t-tau)), tau= T);

Possible to call subfunction in S-function level-2

I have been trying to convert my level-1 S-function to level-2 but I got stuck at calling another subfunction at function Output(block) trying to look for other threads but to no avail, do you mind to provide related links?
My output depends on a lot processing with the inputs, this is the reason I need to call the sub-function in order to calculate and then return output values, all the examples that I can see are calculating their outputs directly in "function Output(block)", in my case I thought it is not possible.
I then tried to use Interpreted Matlab Function block but failed due to the output dimension is NOT the same as input dimension, also it does not support the return of more than ONE output................
Dear Sir/Madam,
I read in S-function documentation that "S-function level-1 supports vector inputs and outputs. DOES NOT support multiple input and output ports".
Does the second sentence mean the input and output dimension MUST BE SAME?
I have been using S-function level-1 to do the following:
[a1, b1] = choose_cells(c, d);
where a1 and b1 are outputs, c and d are inputs. All the variables are having a single value, except d is an array with 6 values.
Referring to the image attached, we all know that in S-function block, the input dimension must be SAME as output dimension, else we will get error, in this case, the input dimension is 7 while the output dimension is 2, so I have to include the "Terminator" blocks in the diagram for it to work perfectly, otherwise, I will get an error.
My problem is, when the system gets bigger, the array d could contain hundreds of variables, using this method, it means I would have to add hundreds of "Terminator" blocks in order to get this work, this definitely does not sound practical.
Could you please suggest me a wise way to implement this?
Thanks in advance.
http://imgur.com/ib6BTTp
http://imageshack.us/content_round.php?page=done&id=4tHclZ2klaGtl66S36zY2KfO5co

Derivative in function

I'd like to write a Mathematica function that takes an expression as argument, takes the derivative of that expression, and then does something to the expression. So (as a toy example) I'd like to write
F[f_] = D[f, x] * 2
so that
F[x^2] = 4x
Instead, I get
F[x^2] = 0
Can someone point me to the relevant docs? I spent some time poking around the Mathematica reference, but didn't find anything helpful.
You've used assignment = when you mean to use delayed assignment :=. When you evaluate F[f_]=D[f,x]*2 using (non-delayed) assignment, Mathematica looks at D[f,x] and sees that f (an unassigned symbol) does not depend on x; hence, its derivative is 0. Thus, F[f_]=0 for any arguments to F, which is what it returns later.
If you want F to be evaluated only after you have specified what f_ should be, you need to use delayed assignment by replacing = with :=.

Finding the Maximum

How to find the following Maximum or supremum by computer software such as Mathematica and Matlab: $\sup\frac{(1+s)^{4}+(s+t)^{4}+t^{4}}{1+s^{4}+t^{4}}$?
Instead of numerical approximation, what is the accurate maximum?
Thanks.
Since the question seems a bit like homework, here's an answer that starts a bit like a lecture:
ask yourself what happens to the function as s and t go to small and to large positive and negative values; this will help you to identify the range of values you should be examining; both Mathematica and Matlab can help your figure this out;
draw the graph of your function over the range of values of interest, develop a feel for its shape and try to figure out where it has maxima; for this the Mathematic Plot3D[] function and the Matlab plot() function will both be useful;
since this is a function of 2 variables, you should think about plotting some of its sections, ie hold s (or t) constant, and make a 2D plot of the section function; again, develop some understanding of how the function behaves;
now you should be able to do some kind of search of the s,t values around the maxima of the function and get an acceptably accurate result.
If this is too difficult then you could use the Mathematica function NMaximize[]. I don't think that Matlab has the same functionality for symbolic functions built-in and you'll have to do the computations numerically but the function findmax will help.
In Matlab, one would create a vector/matrix with s and t values, and a corresponding vector with the function values. Then you can pinpoint the maximum using the function max
In Mathematica, use FindMaximum like this:
f[s_,t_]:= ((1+s)^4 + (s+t)^4 + t^4)/(1+s^4+t^4)
FindMaximum[ f[s,t],{s,0},{t,0} ]
This searches for a maximum starting from (s,t)=(0,0).
For more info, see http://reference.wolfram.com/mathematica/ref/FindMaximum.html