I'm wondering if it's possible to multiplying a function for a rectangularPulse.
I tried to wrote this code, but it's wrong:
Tc = 0.01;
t = [0:Tc:3];
y = t.^2 * rectangularPulse(1,2,t);
Can you tell me if there is a solution (I want to use rectangularPulse, not other function)?
The error is the following:
Error using *
Inner matrix dimensions must agree.
Error in aaa (line 12)
y = t.^2 * rectangularPulse(1,2,t);
Thank you for your time.
The multiplication should be a element-wise multiplication:
y = t.^2 .* rectangularPulse(1,2,t);
Related
I have this code:
function main()
a = 1.0e+04 * [0.005055052938847,0.010897917816899,0.022355965424711,0.043081981439108,0.077074049394439,0.127074049394439,0.193081981439108,0.272355965424711,0.360897917816899,0.455055052938847,0.552256864601221,0.650978664311931,0.750415022011379,0.850172973479352,0.950071110045004,1.050028912038499,1.150011648830086]
B = 1.0e+04 * [1.215101736363023,0.697166188613023,0.400000000000000,0.229500515964941,0.131676217070435,0.075549399394941,0.043346565354951,0.024870147785673,0.014269279372341,0.008187017446000,0.004697311820178,0.002695088715947,0.001546310627203,0.000887197716963,0.000509030834515,0.000292057097908,0.000167568136653
m = timeloop(a,B);
end
function m = timeloop(a,B)
st = zeros(49,4);
t = 0:0.001:0.05;
for i = (1:49)
st(i+1,1:4) = next_state(st(i,1:4),a,B,1e-4);
end
m = mean(prod(state,2))
end
function next_state = next_state(state,alpha,beta,dt)
nch = size(state,2);
p01 = rand(1,nch);
alphadt = repmat(alpha,1,nch)*dt;
betadt = repmat(beta,1,nch)*dt;
next_state1 = (p01<alphadt) .* (state==0);
next_state0 = (p01<betadt) .* (state==1);
next_state = state + next_state1 - next_state0;
end
but it gives me the following error:
Matrix dimensions must agree.
Error in q3>next_state (line 59)
next_state1 = (p01<alphadt) .* (state==0);
Error in q3>timeloop (line 49)
st(i+1,1:4) = next_state(st(i,1:4),a,B,1e-4);
Error in q3 (line 4)
m = timeloop(a,B);
Error in run (line 86)
evalin('caller', [script ';']);
I've tried changing the multiplication to just * with no improvement. What I've looked up online hasn't seemed to be able to help me. I don't understand MATLAB very well so you will have to be very specific in your explanation.
If you debugged your code you would quickly realise the issue.
alpha in next_state is the same as a in main, meaning it is of size 1x3.
You then create alphadt using repmat(alpha,1,4), so it is of size 1x12.
Then you try and do the following
(p01<alphadt) .* (state==0)
% 1x12 .* 1x3
As the error clearly states, your matrix dimensions don't agree.
Because you haven't given any context for what you're trying to achieve, it's not obvious what the solution should be. Perhaps you want to also use repmat on state, or loop over single values from a.
I'm trying to run this function like this: calcSQNRA(0,4,6) and I'm getting these errors:
??? Error using ==> mtimes Inner matrix dimensions must agree.
Error in ==> calcSQNRA>#(x)x.^2*e.^(-x) at 6 f = #(x) x.^2 * e.^(-x);
Error in ==> quadl at 70 y = feval(f,x,varargin{:}); y = y(:).';
Error in ==> calcSQNRA at 7 x = 10 * log10(3 * 4^t *
quadl(f,xmin,xmax));
function [x] = calcSQNRA(xmin, xmax, N)
e = exp(1);
t = log2(N);
f = #(x) x.^2 * e.^(-x);
x = 10 * log10(3 * 4^t * quadl(f,xmin,xmax));
The function is trying to compute the SQNR of an exponential distribution (if I thought of it correctly), after a uniform quantization. Note that if I erase the e.^(-x) from f it actually produces a result. Any ideas?
I kill 15 minutes debugging quadl and feval functions. And going so deep in this process I just think about this simple thing:
it just works if I set . in your f anonymous function this way:
f = #(x) x.^2 .* e.^(-x);
then
calcSQNRA(0,4,6) give result: 22.1635.
Is it what you want to achieve?
I wonder whether MATLAB has a toolbox to do common matrixial operation with sparse matrices.
Using a dense matrix, I can compute the correlogram matrix doing:
R = rand(10,100)
[r,p] = corr(R)
With sparse matrix I would love to do:
S = sprand(10,100,.2)
[r,p] = corr(S)
However, the following error is elicited:
Error using betainc
Inputs must be real, full, and double or single.
Error in tcdf (line 70)
p(t) = betainc(xsq(t) ./ (v(t) + xsq(t)), 0.5, v(t)/2, 'upper') / 2;
Error in corr>pvalPearson (line 720)
p = 2*tcdf(-abs(t),n-2);
Error in corr>corrPearson (line 321)
pval(ltri) = pvalPearson(tail, coef(ltri), n);
Error in corr (line 204)
[coef,pval] = corrFun(rows,tail,x);
Can anyone help me?
Cmon ppl, let's do some math! Let x be a random vector. An entry in the correlation matrix CORR(x_i, x_j) is given by:
CORR(x_i, x_j) = COV(x_i, x_j) / (SQRT(VAR(x_i)) *SQRT(VAR(x_j));
That is, to build our correlation matrix, we need the covariance matrix, which also gives us the individual variances. Formula for covariance: COV(x) = E[x*x'] - E[x]E[x]'. We can then approximate the the population moments E[x*x'] with the sample moments (i.e. X'*X/n and mean(X))
Hence the following Matlab code:
[n, k] = size(X);
Exxprim = full(X'*X)/n; %I'm shocked if this isn't full so let's drop sparse now
Ex = full(mean(X))'; %same deal
COVX = (Exxprim - Ex*Ex');
STDEVX = sqrt(diag(COVX));
CORRX = COVX ./ (STDEVX * STDEVX');
This may help if X'*X and mean(X) can be done more efficiently because X is sparse.
I'm trying to minimize an objective function that contains absolute terms including some of the variables. The target function looks like this (I'll only write down two terms, the actual problem contains between 500 and 5000, depending on other parameters):
min |f_11 * x_1 + f_21 * x_2 - y_1| + |f_12 * x_1 + f_22 * x_2 - y_2|
There can also be different types of constraints. Since I don't have the Symbolic Toolbox I have no clue how to put this into Matlab.
I thought of interpreting this as a quadratic program, where I square each term and get the squareroot of it. With an anonymous function this would look like this I think:
f = #(X) sqrt((F*X - Y) .* (F*X - Y)) * ones(size(Y));
Where F and Y contain the values of f_ij and y_j. So in my case F ís of size ix2, Y is of size ix1 and X is of size 1x2.
The problem here is, I can't calculate the numerical hessian via the DERIVESTsuite (http://www.mathworks.com/matlabcentral/fileexchange/13490-adaptive-robust-numerical-differentiation). I'll get the error:
Error using *
Inner matrix dimensions must agree.
Error in calcHq>#(W)sqrt((F*W-Y).*(F*W-Y))*ones(size(Y)) (line 16)
f = #(W) sqrt((F*W - Y) .* (F*W - Y)) * ones(size(Y));
Error in hessdiag>#(xi)fun(swapelement(x0,ind,xi)) (line 60)
#(xi) fun(swapelement(x0,ind,xi)), ...
Error in derivest (line 337)
f_x0(j) = fun(x0(j));
Error in hessdiag (line 59)
[HD(ind),err(ind),finaldelta(ind)] = derivest( ...
Error in hessian2 (line 74)
[hess,err] = hessdiag(fun,x0);
I assume there is some problem with the elementwise multiplication, but I really can't figure out what I'm doing wrong.
Maybe someone can give me a hint on what I'm doing wrong.
Ok guys, thank you very much. I just found out what I did wrong and it is so embarassing ...
The order of multiplications is wrong ...
f = #(X) sqrt((F*X - Y) .* (F*X - Y)) * ones(size(Y));
This gives back an ixi matrix, while
f = #(X) ones(size(Y)) * sqrt((F*X - Y) .* (F*X - Y));
gives back a scalar.
I have to solve a system of ordinary differential equations of the form:
dx/ds = 1/x * [y* (g + s/y) - a*x*f(x^2,y^2)]
dy/ds = 1/x * [-y * (b + y) * f()] - y/s - c
where x, and y are the variables I need to find out, and s is the independent variable; the rest are constants. I've tried to solve this with ode45 with no success so far:
y = ode45(#yprime, s, [1 1]);
function dyds = yprime(s,y)
global g a v0 d
dyds_1 = 1./y(1) .*(y(2) .* (g + s ./ y(2)) - a .* y(1) .* sqrt(y(1).^2 + (v0 + y(2)).^2));
dyds_2 = - (y(2) .* (v0 + y(2)) .* sqrt(y(1).^2 + (v0 + y(2)).^2))./y(1) - y(2)./s - d;
dyds = [dyds_1; dyds_2];
return
where #yprime has the system of equations. I get the following error message:
YPRIME returns a vector of length 0, but the length of initial
conditions vector is 2. The vector returned by YPRIME and the initial
conditions vector must have the same number of elements.
Any ideas?
thanks
Certainly, you should have a look at your function yprime. Using some simple model that shares the number of differential state variables with your problem, have a look at this example.
function dyds = yprime(s, y)
dyds = zeros(2, 1);
dyds(1) = y(1) + y(2);
dyds(2) = 0.5 * y(1);
end
yprime must return a column vector that holds the values of the two right hand sides. The input argument s can be ignored because your model is time-independent. The example you show is somewhat difficult in that it is not of the form dy/dt = f(t, y). You will have to rearrange your equations as a first step. It will help to rename x into y(1) and y into y(2).
Also, are you sure that your global g a v0 d are not empty? If any one of those variables remains uninitialized, you will be multiplying state variables with an empty matrix, eventually resulting in an empty vector dyds being returned. This can be tested with
assert(~isempty(v0), 'v0 not initialized');
in yprime, or you could employ a debugging breakpoint.
the syntax for ODE solvers is [s y]=ode45(#yprime, [1 10], [2 2])
and you dont need to do elementwise operation in your case i.e. instead of .* just use *