Suppose the function of 3 variables is F(T,x,y)=ln(T-x)exp(T+y^3), I want to compute things like
H(T)=\int_0^T \int_0^x ln(T-x)exp(T+y^3) dy dx
What I want is that for different values of T, say 0:0.25:2, I can obtain H(0:0.25:2) immediately. The code I used is as follow:
fun=#(T,x,y) ln(T-x)exp(T+y^3)
ymax=#(x)x
H=#(T)arrayfun(#(s)integral2(fun,0,s,0,ymax),T)
But it doesn't work. Matlab gives error. How to solve this problem?
In Matlab, natural log is not ln(), but log().
First of all,change your function as below pattern.
fun=#(x,y) log(T-x).*exp(T+y.^3)
Here, I used an operator * for product, and added . in front of * and ^ in order to describe matrix product.
One example is below, for getting integral according to T.
T=0:0.25:5;
N=length(T);
all_H=zeros(1,N);
s=2;
for k=1:N;
fun=#(x,y) log(T(k)-x).*exp(T(k)+y.^3)
ymax=#(x)x
H=integral2(fun,0,s,0,ymax)
all_H(k)=H;
end
plot(abs(all_H))
Related
Hi, I am trying to write a function as per the question. I have tried to create four sub-matrices which are the reverse of each other and then multiply to give the products demanded by the question. My attempt:
function T = custom_blocksT(n,m)
T(1:end,end-1:1);
T(1:end,end:-1:1)*2;
T(1:end,end:-1:1)*3;
T(1:end,end:-1:1)*4;
What I'm unsure of is
(i) What do the the indivual sub-matrices(T(1:end,end-1:1);)need to be equal to? I was thinking of(1:3)?
(ii) I tried to create a generic sub-matrix which can take any size matrix input using end was this correct or can't you do that? I keep getting this error
Undefined function or variable 'T'.
Error in custom_blocksT (line 2)
T(1:end,end-1:1);
I have searched the Matlab documentation and stacked overflow, but the problem is I'm not quite sure what I'm supposed to be looking for in terms of solving this question.
If someone could help me I would be very thankfull.
There are many problems with your function:
function T = custom_blocksT(n,m)
T(1:end,end-1:1);
T(1:end,end:-1:1)*2;
T(1:end,end:-1:1)*3;
T(1:end,end:-1:1)*4;
end
This is an extremely basic question, I highly recommend you find and work through some very basic MATLAB tutorials before continuing, even before reading this answer to be honest.
That said here is what you should have done and a bit of what you did wrong:
First, you are getting the error that T dos not exist because it doesn't. The only variables that exist in your function are those that you create in the function or those that are passed in as parameters. You should have passed in T as a parameter, but instead you passed in n and m which you don't use.
In the question, they call the function using the example:
custom_blocks([1:3;3:-1:1])
So you can see that they are only passing in one variable, your function takes two and that's already a problem. The one variable is the matrix, not it's dimensions. And the matrix they are passing in is [1:3;3:-1:1] which if you type in the command line you will see gives you
[1 2 3
3 2 1]
So for your first line to take in one argument which is that matrix it should rather read
function TOut = custom_blocks(TIn)
Now what they are asking you to do is create a matrix, TOut, which is just different multiples of TIn concatenated.
What you've done with say TIn(1:end,end-1:1)*2; is just ask MATLAB to multiple TIn by 2 (that's the only correct bit) but then do nothing with it. Furthermore, indexing the rows by 1:end will do what you want (i.e. request all the rows) but in MATLAB you can actually just use : for that. Indexing the columns by end-1:1 will also call all the columns, but in reverse order. So in effect you are flipping your matrix left-to-right which I'm sure is not what you wanted. So you could have just written TIn(:,:) but since that's just requesting the entire matrix unchanged you could actually just write TIn.
So now to multiply and concatenate (i.e. stick together) you do this
TOut = [TIn, TIn*2; TIn*3, TIn*4]
The [] is like a concatenate operation where , is for horizontal and ; is for vertical concatenation.
Putting it all together:
function TOut = custom_blocks(TIn)
TOut = [TIn, TIn*2; TIn*3, TIn*4];
end
I first defined functions for dy/dt=y and dy/dt=t:
function dy=d(y):
dy=y
end
function ddy=dd(t):
ddy=t
end
And then I used ode45, respectively:
[t,y]=ode45('d',[1 10],1)
[t,y]=ode45('dd',[1 10],1)
which returns the following error: Error using d
Too many input arguments.
My question is:
Where did I go wrong?
How does Matlab know whether y or t is the independent variable? When I define the first function, it could be reasonably interpreted as dt/dy=y instead of dy/dt=y. Is there a built-in convention for defining functions?
First things first: the docs on ode45 are on the mathworks website, or you can get them from the console by entering help ode45.
The function you pass in needs to take two variables, y then t. As you noticed, with just one it would be impossible to distinguish a function of only y from a function of only t. The first argument has to be the independent, the second is the dependent.
Try defining your function as dy = d(t, y) and ddy = dd(t, y) with the same bodies.
one other note, while using a string representing the function name should work, you can use #d and #dd to reference the functions directly.
I want to modify a simple function that solves quadratics so that if there is a repeated root it will only output one of them. I have named x1, x2 as my two outputs - how do I 'supress' one of them in the case of a repeated root?
I've tried x1=x2= -b/(2*a) but it comes up with the error 'The expression to the left of the equals sign is not a valid target for an assignment.' Why doesn't this work? And how can i get it to work?
There are many ways to solve this. The best is probably to output a single variable, an array or a cell, that contain a variable number of elements corresponding to your roots.
In your case this gives something like:
function out = myfunction(...)
...
if x1==x2
out = x1
else
out = [x1 x2];
end
You can of course modify it to take also into account the cases where there is no solution.
Also, you should know that there exists a built-in function that finds all polynomial roots for you: it is roots.
Best,
I have a function M file defined as follows:
function [v,m ] = myfun(y)
m=mean(y);
v=var(y);
end
For a given vector which consists of integers from 1 to 100 for simplicity, I want to do bootstrap for 10 times and obtain both mean and variance for each bootstrapped sample. The following wouldn't work:
y=[1:100]';
[m,v]=bootstrp(10,#(x) myfun(x),y);
Could any one help me out of here? Thanks in advance!
Why don't you think it works? This does exactly what you're specifying. However, I would do away with specifying a separate function and putting the mean and standard deviation directly in the anonymous function itself. Specifically:
stats = bootstrp(10, #(x) [mean(x) var(x)], y);
In this case, you will get a 10 x 2 matrix. The first column will give you the mean of each boostrapped sample while the next column will give you the variance of each bootstrapped sample. Specifically, the first row gives you the mean (first column) and variance (second column) of the first sample. The second row gives you the mean and variance of the second sample, and so on. Each column of your output in stats will give you whatever measure you are calculating in the corresponding position in the output vector of your function.
Check the documentation for bootstrp here: http://www.mathworks.com/help/stats/bootstrp.html
To answer your question as to why you're getting the too many outputs error is because you need to output only one variable, but you are outputting two. As such, group your variables into a single vector like so:
function [out] = myfun(y)
m=mean(y);
v=var(y);
out = [m,v];
end
If you now run your bootstrp code with this function, it should now work.
I have the following code:
syms t x;
e=symfun(x-t,[x,t]);
In the problem I want to solve x is a function of t but I only know its value at the given t,so I modeled it here as a variable.I want to differentiate e with respect to time without "losing" x,so that I can then substitute it with x'(t) which is known to me.
In another question of mine here,someone suggested that I write the following:
e=symfun(exp(t)-t,[t]);
and after the differentiation check if I can substitute exp(t) with the value of x'(t).
Is this possible?Is there any other neater way?
I'm really not sure I understand what you're asking (and I didn't understand your other question either), but here's an attempt.
Since, x is a function of time, let's make that explicit by making it what the help and documentation for symfun calls an "abstract" or "arbitrary" symbolic function, i.e., one without a definition. In Matlab R2014b:
syms t x(t);
e = symfun(x-t,t)
which returns
e(t) =
x(t) - t
Taking the derivative of the symfun function e with respect to time:
edot = diff(e,t)
returns
edot(t) =
D(x)(t) - 1
the expression for edot(t) is a function of the derivative of x with respect to time:
xdot = diff(x,t)
which is the abstract symfun:
xdot(t) =
D(x)(t)
Now, I think you want to be able to substitute a specific value for xdot (xdot_given) into e(t) for t at t_given. You should be able to do this just using subs, e.g., something like this:
sums t_given xdot_given;
edot_t_given = subs(edot,{t,xdot},{t_given, xdot_given});
You may not need to substitute t if the only parts of edot that are a function of time are the xdot parts.