Multiplying two Fourier transforms in MATLAB - matlab

When I want to multiply two Fourier transforms there is an error. I don't know why. I use new var add the two then multiply them -> .*
%a
n=1:1:20;
h=3*((-1/4).^n).*(n>=0);
x= cos(3*pi*n/4)+(pi/8);
subplot(4,1,1);
stem(n,x);
grid;
xlabel('n');
ylabel('x3(n)');
title('x3(n)');
y3=conv(x,h);
y = fft(x);
subplot(4,1,2);
stem(y);
yy = fft(y3);
subplot(4,1,3);
stem(yy);
zzz=y .* yy;
error: product: nonconformant
arguments (op1 is 1x20, op2 is 1x39)
;
stem(zzz);
subplot(4,1,4);
stem(zzz);
First fft is y
Second ftt is yy
new var to multiply zzz

Related

Extracting Z values after 3D plot is generated by 2D curve revolution with repmat

I am stuck with an apparently simple problem. I have to revolve of 360° a 2D curve around an axis, to obtain a 3D plot. Say, I want to do it with this sine function:
z = sin(r);
theta = 0:pi/20:2*pi;
xx = bsxfun(#times,r',cos(theta));
yy = bsxfun(#times,r',sin(theta));
zz = repmat(z',1,length(theta));
surf(xx,yy,zz)
axis equal
I now want to visualize the numerical values of the Z plane, stored in a matrix. I would normally do it this way:
ch=get(gca,'children')
X=get(ch,'Xdata')
Y=get(ch,'Ydata')
Z=get(ch,'Zdata')
If I visualize Z with
imagesc(Z)
I don't obtain the actual values of Z of the plot, but the "un-revolved" projection. I suspect that this is related to the way I generate the curve, and from the fact I don't have a function of the type
zz = f(xx,yy)
Is there any way I can obtain the grid values of xx and yy, as well as the values of zz at each gridpoint?
Thank you for your help.
Instead of bsxfun you can use meshgrid:
% The two parameters needed for the parametric equation
h = linspace(0,2) ;
th = 0:pi/20:2*pi ;
[R,T] = meshgrid(h,th) ;
% The parametric equation
% f(x) Rotation along Z
% ↓ ↓
X = sin(R) .* cos(T) ;
Y = sin(R) .* sin(T) ;
% Z = h
Z = R ;
surf(X,Y,Z,'EdgeColor',"none")
xlabel('X')
ylabel('Y')
zlabel('Z')
Which produce:
And if you want to extract the contour on the X plane (X = 0) you can use contour:
contour(Y,Z,X,[0,0])
Which produce:

Lagrange interpolation perturbation in matlab

This is my code for finding the centered coefficients for lagrange polynomial interpolation:
% INPUT
% f f scalar - valued function
% interval interpolation interval [a, b]
% n interpolation order
%
% OUTPUT
% coeff centered coefficients of Lagrange interpolant
function coeff = lagrangeInterp (f, interval , n)
a = interval(1);
b = interval(2);
x = linspace(a,b,n+1);
y = f(x);
coeff(1,:) = polyfit(x,y,n);
end
Which is called in the following script
%Plot lagrangeInterp and sin(x) together
hold on
x = 0:0.1*pi:2*pi;
for n = 1:1:4
coeff = lagrangeInterp(#(x)sin(x),[0,2*pi],n);
plot(x,polyval(coeff,x,'-'));
end
y = sin(x);
plot(x,y);
legend('1st order','2nd order','3rd order','4th order','sin(x)');
To check for stability I would like to perturb the function (eg g(x) = f(x) + epsilon). How would I go about this?
Well, a little trick for you.
You know randn([m,n]) in matlab generate a m*n random matrix. The point is to generate a random vector, and interp1 to a function of x. Like this:
x = linspace(a,b,n+1); % Your range of input
g = #(ep,xx)f(xx)+interp1(x,ep*randn([length(x),1]),xx);

{OFF} How can I derivate a spline interpolation function on matlab?

I have lab test data from a battery discharge curve. The data consistes in 22 points of voltage versus time. In matlab I have traced an interpolation curve through spline interpolation, but I wish to make the derivative of this plot, how can I do this?
enter image description here
Code
x = [0; 3600 ;7200 ;10800; 14400; 18000; 21600; 25200 ;28800;...
32400; 36000 ;39600 ;43200 ;46800; 50400 ;54000; 57600; 61200;...
64800 ;68400 ;72000; 74880];
y = [12.75; 12.40; 12.38; 12.34; 12.30; 12.26 ;12.21 ;12.17 ;...
12.12; 12.07; 12.02 ;11.97 ;11.91 ;11.85; 11.79; 11.72; 11.65;...
11.56 ;11.46 ;11.35 ;11.17; 10.59];
f = fit( x, y,'cubicinterp')
You can use gradient of y/x:
f = fit( x, y,'cubicinterp')
df = gradient(f(x)); % f'x
dx = gradient(x); % dx
dfx = df ./ dx;
plot(x, y, x, dfx);

Get derivative numerically

I have 2 vectors and a scalar:
grid which is (N x 1);
Value which is (N x 1);
sval which is (1,1);
If I want to interpolate sval on grid I know that I can simply do:
intervalue = interp1(grid, Value, sval, 'PCHIP');
What if now I want the derivatives, i.e. the slope of the function Value at that particular point sval?
As mentioned in the comments, you can approximate the derivative by forward finite difference approximation:
slope = diff(Value) ./ diff(grid);
Alternatively:
slope = gradient(Value(1:end-1),grid);
This is a simple method of numerical differentiation. For a detailed guide on numerical differentiation in MATALB, see this answer.
Here is an example of the finite difference method with the desired interpolation:
% Define function y = x^3
grid = 1:100;
Value = grid .^ 3;
% Approximate derivative via the local slope
slope = diff(Value) ./ diff(grid);
% Or: slope = gradient(Value(1:end-1),grid);
slope_grid = grid(1:end-1);
% Interpolate derivative
sval = 33.5;
sval_slope = interp1(slope_grid, slope, sval, 'PCHIP');
We can visualize the result:
figure;
plot(grid, 3*grid.^2)
hold on
plot(slope_grid, slope)
legend('Reference', 'Approximation', 'Location', 'NorthWest')

Matlab error , matrix dimensions dont agree

im trying to plot 2 figures, using fplot and plot functions, but for my plot (fig2) , i get an error and don't understand why;
Error using /
Matrix dimensions must agree.
Error in bhpfilter (line 9)
H = 3*g / ( (fo/f).^2 + 3*(fo/f)+3);
Error in #(f)bhpfilter(f,fo,g)
function [H] = bhpfilter(f, fo, g)
%freq finds the filter frequency response in V/V
%fo is the cut off frequency, f is the input frequency and g is the filter
%gain
if fo <= 0 || g <=0 %error checking
error('Inputs invalid');
else
H = 3*g / ( (fo/f).^2 + 3*(fo/f)+3);
end
fo=1200.;
g=2.;
H =#(f) bhpfilter(f,fo,g);
H_1 = #(f) bhpfilter (f,fo,g)-0.8;
figure (1);
fplot(H,[0 2000]);
title('Plot of H vs f using fplot');
xlabel('Frequency (Hz)');
ylabel('Filter frequency response (V/V)');
fprintf('The value of f that gives a response of 0.8 is %f Hz\n',fzero (H_1, [0 2000])); %placed this line of code here so that it can be visible in command window , showing it works
figure (2);
plot([0:2000],H([0:2000])); % code will find individual values of H(1), H(2) etc.. but will not find H([0:200])
title('Plot of H vs f using plot');
xlabel('Frequency (Hz)');
ylabel('Filter frequency response (V/V)');
In the line H = 3*g / ( (fo/f).^2 + 3*(fo/f)+3); g and fo are scalars while f is a vector. For division MATLAB doesn't recognize the divide operator as element by element division when it is scalar/vector (it does for the other way around). You have to put:
H = 3*g ./ ( (fo./f).^2 + 3*(fo./f)+3);
Hope that helps.