I'm having trouble calculating a series on MATLAB. I'm trying to use the symbolic toolbox, but MATLAB keeps busy and never returns my result. This is my code:
function Y = ProbCollision_S (n,tp,s,T)
syms k;
format long;
Y = double(symsum(exp(-n.*s./T).*((n.*s./T).^k).*(1-(1-k.*tp./s).^k)./factorial(k),2,Inf));
end
Please help?
Thank you in advance.
Related
I want to use the sinc function to interpolate some data I already have sampled. I am not very familiar with Matlab, but I found the following script:
rng default
t = 1:10;
x = randn(size(t))';
ts = linspace(-5,15,600);
[Ts,T] = ndgrid(ts,t);
y = sinc(Ts - T)*x;
plot(t,x,'o',ts,y)
xlabel Time, ylabel Signal
legend('Sampled','Interpolated','Location','SouthWest')
legend boxoff
I understand how to change the formatting.
My question is, how can I do this for my own sampled data?
Any help would be greatly appreciated. Thank you very much!
Edit: I tried replacing the x-values with my own multiple times, but each time it reverts to the original when I run the code. I'm not sure why this happens...
I've got a convolution where the final result is
y=(-t/2)+5t=6
Is there any chance to check this in matlab but not through convolution, I have programmed that part. What I am wondering is it possible to plot the signal using this equation and compare it with the one that I got with coding convolution.
You can plot functions easily in matlab: look at the examles from here.
For example using this code:
t = 0:.1:10
plot(t,(-t/2)+5*t)
will plot you your function between the values x = [0, 10].
I have the following minimal code:
N=30;
P=200;
a = lpc(signal,N);
y = zeros(1, P);
y(1:N) = x(1:N);
for ii=(N+1):P
y(ii) = -sum(a(2:end) .* y((ii-1):-1:(ii-N)));
end
the for loop in y is not efficient, is the a way to vectorize this? maybe a matlab related function ?
EDIT:
some more context to the question - I am trying to predict a known periodic signal efficiently using lpc. For a=lpc(signal,3) I found in matlab documentation that y=filter([0 -a(2:end)],1,x) would do, how do I generalize it to lpc(signal,N)?
I used the symbolic toolbox to print out the formula for any later y values. These formulas are very long and require about (ii-N)*N multiplications for step ii to calculate y directly. A vectorised solution would have to do all these multiplications, it will be slower.
Optimizing your loop is everything that can be done:
b=a(end:-1:2);
for ii=(N+1):P
y(ii) = -sum(b .* y((ii-N):(ii-1)));
end
Indexing backwards is slow.
I don't see an easily feasible way, as each position in y depends on its precedessors. So they have to be calculated step by step.
I'm having some trouble trying to solve and plot an integral in matlab. In fact, I know that if a solve one, I'll solve all the integrals that I need now.
I have plot in x axis a value of a variable "d" and in y axis the value of a integral of a normalized gaussian function from -inf to ((40*log10(d)-112)/36) and I'm not finding out a way to do it correctly. D is between 0 and 1600
Can anybody here please help me?
In Matlab you can use the integral-function to evaluate integrals:
q = integral(fun,xmin,xmax)
fun needs to be a function handle, also called function functions, like these two examples:
square = #(x) x.^2;
plusone = #(x) x+1;
I have tried to plot this function:
t=linspace(0,2*pi,100);
a=input('a= ');
b=input('b= ');
c=input('c= ');
k = a*(1-(sin(3*t)).^(2*b))+c;
polar(t,k)
% a=2.6
% b=0.4
% c=5
Each time, I get the following message:
Warning: Imaginary parts of complex X and/or Y arguments ignored.
I have tried the pol2cart method as such:
t=linspace(0,2*pi,100);
a=input('a= ');
b=input('b= ');
c=input('c= ');
k = a*(1-(sin(3*t)).^(2*b))+c;
[x,y] = pol2cart(t,k);
plot(x,y)
I got the same message again.
I have tried to convert it to spherical coordinates, it didn't work. I have also tried the arrayfun method as suggested in a forum answer, it didn't work as well.
Can someone please help me?
Thank you!
Your problem is in your function. k contains imaginary numbers, because of this:
sin(3*t).^(0.8)
If you want to make sure it doesn't contain imaginary numbers, you need to increase b. Bottom line is, fix your formula. I can only suppose you mean something like this, but there could be other solutions. Essentially, I think you mean to take the exponent of 1-sin, not sin.
k=a*((1-sin(3*t)).^(2*b))+c;
This gives the following plot (From Octave, but it should be the same)
I figured this out by `plot(k). If k contains imaginary, it will plot the real vs imaginary components. If it is purely real, it will plot the line vs time.