difficulty using deconv command - matlab

I am writing code in MATLAB for convolution and then deconvolution
But when i try to run/simulate, i get following error;
Error using deconv (line 19)
First coefficient of A must be non-zero.
Error in Untitled (line 10)
x1=deconv(y,h)
The code that i am using is as follow:
clc, clear all, close all
t=[0:0.1:36]
h=exp(-2*t).*heaviside(t-1)
x=heaviside(t)-heaviside(t-2)
y=conv(x,h)
plot(y)
xlabel('Time')
ylabel('Amplitude')
title('y(t)=x(t)*h(t)')
x1=deconv(y,h)
How can i get out of this error, as i have to find back input x,by using deconv command which takes two arguments(output y and impulse response h)

Related

Polyval issues after using polyint

Im trying to calculate the area of randomly generated graph, which is created from randomly generated x and y values and drawn using polyfit.
clear
clc
for i=1:8
x(i)= round((12+5).*rand - 5,0)
y(i)= round((7+6).*rand -6,0)
end
p=polyfit(x,y,5);
x1=-5:0.1:12;
y1=polyval(p,x1);
plot(x,y,'o')
hold on
plot(x1,y1)
y2=(x1)*0-5
plot(x1,y2)
hold off
syms x
S1=int(x.*0-5,x,-2,7)
pp=polyint(p,x)
S2=polyval(pp,-2)-polyval(pp,7)
S=S1+S2
However, I am getting this weird error that doesnt make any sense to me.
Undefined function 'filter' for input arguments of type 'sym'.
Error in polyval (line 56)
y = filter(1,[1 -x],p);
Why doesnt it allow me to use polyval after using polyint ? Its still a polynomial..
In other words. How could I change the end of the code to calculate the definite integral of the newly formed polynomial, which is always different

Matlab , Error while plotting Heaviside. (Vectors must be the same length.)

I am doing a problem on Hamming code in Matlab. I have generated a bit string of length 1190, for transmission. I am asked to display the string as a curve of step function.
After doing some researched i found that the Heaviside function can be used for display the bit string as unit step curve.
When I use the command fplot(heaviside(l_f),[0 ,10000]), to plot the curve, where l_f is the bit string of length 1190, I get this error
Error using fcnchk (line 106)
FUN must be a function, a valid string expression, or an inline
function object.
Error in fplot (line 60)
fun = fcnchk(fun);
Error in Untitled (line 88)
fplot(heaviside(l_f),[0 ,10000])
When i display using Plot, i.e plot(heaviside(l_f),[0 ,10000]), I get the error
Error using plot Vectors must be the same length.
Error in Untitled (line 88) plot(heaviside(l_f),[0 ,10000])
Anyway to plot the bit string as a curve of step function ?
fplot(heaviside(l_f),[0 ,10000]) won't plot since fplot requires a function as first parameter. But in here it is a matrix. So use plot instead. Next, dimension of heaviside(l_f) will be 1x1190 and dimension of [0 ,10000] is 1x2. So wont work since dimensions are different so use.
x=heaviside(l_f)
y=0:(10000+1)/length(l_f):10000;
plot(x,y);

how to use streamline in matlab correctly

i have problem using streamline in matlab. my problem is i have vector field that compute u,v,x,y from a loop with a specific math function. i use this code to figure vector field :
quiver(x,y,u,v)
and it is get good answer and my answer is converge. but when i want to figure streamline with this code:
startx=-100;
starty=-100;
streamline(x,y,u,v,startx,starty)
i got this error:
Error using griddedInterpolant
Interpolation requires at least two sample points in each dimension.
Error in interp1 (line 183)
F = griddedInterpolant(X,V,method);
Error in stream2 (line 62)
sxi=interp1(xx(:),1:szu(2),sx(k));
Error in streamline (line 62)
verts = stream2(x,y,u,v,sx,sy,options);
Error in (line 44)
streamline(x,y,u,v,startx,starty)
thanks in advance.
Best Regard

not enough input arguments fminsearch

I'm trying to write a script in MATLAB that graphs a function in three dimensions using the mesh function and then finds the maximum of the surface. This is my code so far:
%% Aquifer, 3D maximum search
figure(2)
[X,Y] = meshgrid(-10:.5:10,-10:.5:10);
h = #(x,y)-(1./(1+(x-.25).^2+(y-.5).^2+x+x.*y));
mesh(h(X,Y)) %graph aquifer surface
aquamax = fminsearch(h,[-5;-5])
When I run the code I get this error:
Error using #(x,y)-(1./(1+(x-.25).^2+(y-.5).^2+x+x.*y))
Not enough input arguments.
Error in fminsearch (line 190)
fv(:,1) = funfcn(x,varargin{:});
I've read up on the fminsearch function but I'm not that familiar with it (still a bit of a noob at Matlab). Do I need to rework the code or is it just how I've input things into fminsearch?
Your h function requires 2 scalar inputs, but fminsearch only does one input, possibly a vector. Change h to h = #(x)-(1./(1+(x(1)-.25).^2+(x(2)-.5).^2+x(1)+x(1).*x(2))); and see if that works.

Woking with colfit in matlab

I wanted to calculate the histogram n local histogram equalisation of an input image, using colfit. But when i run the code i get the following error.
??? Error using ==> ge
Matrix dimensions must agree.
Error in ==> colfilt at 135
if all(block>=size(a)), % Process the whole matrix at once.
Error in ==> localhist at 10
z=colfilt(f,[w w],'sliding',#std);
Please provide some insights.
I haven't seen it written on the documentation (neither on help colfilt nor on docs colfilt), but I think you can only use colfilt, as nlfilter, with monochannel images. So that if you try to run the example code provided on help colfilt on a 3-channel image, say:
I = imread('peppers.png'); % 'peppers.png' is just a demo color image usually provided with matblab
figure, imshow(I)
I2 = uint8(colfilt(I,[5 5],'sliding',#mean));
figure, imshow(I2)
You get the kind of error that you posted:
Error using >=
Matrix dimensions must agree.
Error in colfilt (line 135)
if all(block>=size(a)), % Process the whole matrix at once.
If you thry this instead, which only takes the first channel (or any other combination of the channels) it will just work
% which is one of the example images usually provided with matlab
J = imread('peppers.png');
I = J(:,:,1);
figure, imshow(I)
I2 = uint8(colfilt(I,[5 5],'sliding',#mean));
figure, imshow(I2)
I hope this helps