Good day!
I hope anyone here knows MATLAB. I'm very new to it and I was just doing a practice. Anyways, I'm having trouble plotting the function in 2D and 3D of my exponential Laplace transform. Here's my code so far.
close all
clc
syms t;
u = input("Enter u: ");
a = input("Enter alpha: ");
f = u*exp(a*t);
F = laplace(f);
disp("The Laplace version")
disp(F)
t = 0:5:5;
p = subs(F);
plot(t,p)
Thanks for your help, in advance.
Related
I've got this assignment to implement a 1D DCT-II myself in Matlab where the 1D DCT-II of an even length sequence is defined as:
Naturally, using the built-in dct function is prohibited whilethe fft functions are available to me. Using the relation between DCT-II and DFT I've computed the DCT coefficients from the DFT coefficients of the even and symmetric extension of the original sequence as follows:
However, my own implementation doesn't agree with the build-in dct function. I've seen a couple of questions about implementation of DCT but was unable to find the problem in my own code. My code follows.
function X_dct = dct_new (x_sig)
N = length(x_sig);
if mod(N,2) ~= 0
error('Sequence is of odd length.');
end
x_hat = zeros(N, 1);
for n = 1: (N/2)
x_hat(n) = x_sig((2*n)-1);
x_hat(N-n+1) = x_sig(2*n);
end
X_hat_dft = fftshift(fft(x_hat));
X_dct = zeros(1, N);
for k = 1:N
X_dct(k) = real(alpha(k-1,N)* exp(-1i*pi*(k-1)/(2*N))*(X_hat_dft(k)));
end
end
function a = alpha(k, N)
if k == 0
a = sqrt(1/N);
else
a = sqrt(2/N);
end
end
Thanks in advance.
As pointed out by Cris Luengo in his comment my mistake was the usage of fftshift before computing the dct coefficients as I didn't take the shift position.
I'm trying to plot the boundaries of Arnold tongues (the regions were periodic solutions exist) for the circle map, f(x) = 2x + a + b*sin(2*pi*x)/pi mod 1. These are defined when f^n(x)=x and d/dx(f^n(x)) = 1, where f^n(x) represents iterating the function n times, i.e. f^2(x) = f(f(x)), n is the period of the periodic point.
I would like to able to take the two equations and write an equation for the boundary of the Arnold tongues in terms of b, so I will get x = g(b) and a = h(b) which satisfies the equation. I then want to plot a against b.
Analytically I can solve this in this way for the period 1 boundary by rearrange d/dx(f(x)) = 1 for x which gives x in terms of b then substituting this value into f(x) = x to give a in terms of b. I've also managed to do this in MATLAB using symbolic equations in the following way.
clear;
syms x a b
f = 2*x + a + (b/pi)*sin(2*pi*x);
g = diff(f,x);
solx = solve(g==1,x);
fnox = subs(f,x,solx);
solb(1) = solve(fnox(1)==solx(1), a);
solb(2) = solve(fnox(2)==solx(2),a);
[xval1,yval1] = fplot(matlabFunction(solb(1)),[0 1]);
[xval2,yval2] = fplot(matlabFunction(solb(2)),[0 1]);
A1 = [xval1,yval1];
A2 = [xval2,yval2];
A1 = A1(imag(A1(:,2))==0,:);
A2 = A2(imag(A2(:,2))==0,:);
figure(1)
hold on;
plot(A1(:,2),A1(:,1),'b')
plot(A2(:,2),A2(:,1),'b')
hold off;
The question is this, is there a way for me to solve for period 2 or higher boundary? I've tried the following,
f2 = subs(f,x,f)
g2 = diff(f2,x)
solx2 = solve(g2==1,x);
However I get a 'cannot find explicit solution' warning. I think perhaps the equation is too complicated for MATLAB to solve symbolically. Is there a way I can get it to work using symbolic equations? If not is there a suitable numeric method to perform the above?
Any help is much appreciated, thanks in advance.
I have an ellipse in 2 dimensions, defined by a positive definite matrix X as follows: a point x is in the ellipse if x'*X*x <= 1. How can I plot this ellipse in matlab? I've done a bit of searching while finding surprisingly little.
Figured out the answer actually: I'd post this as an answer, but it won't let me (new user):
Figured it out after a bit of tinkering. Basically, we express the points on the ellipse border (x'*X*x = 1) as a weighted combination of the eigenvectors of X, which makes some of the math to find the points easier. We can just write (au+bv)'X(au+bv)=1 and work out the relationship between a,b. Matlab code follows (sorry it's messy, just used the same notation that I was using with pen/paper):
function plot_ellipse(X, varargin)
% Plots an ellipse of the form x'*X*x <= 1
% plot vectors of the form a*u + b*v where u,v are eigenvectors of X
[V,D] = eig(X);
u = V(:,1);
v = V(:,2);
l1 = D(1,1);
l2 = D(2,2);
pts = [];
delta = .1;
for alpha = -1/sqrt(l1)-delta:delta:1/sqrt(l1)+delta
beta = sqrt((1 - alpha^2 * l1)/l2);
pts(:,end+1) = alpha*u + beta*v;
end
for alpha = 1/sqrt(l1)+delta:-delta:-1/sqrt(l1)-delta
beta = -sqrt((1 - alpha^2 * l1)/l2);
pts(:,end+1) = alpha*u + beta*v;
end
plot(pts(1,:), pts(2,:), varargin{:})
I stumbled across this post while searching for this topic, and even though it's settled, I thought I might provide another simpler solution, if the matrix is symmetric.
Another way of doing this is to use the Cholesky decomposition of the semi-definite positive matrix E implemented in Matlab as the chol function. It computes an upper triangular matrix R such that X = R' * R. Using this, x'*X*x = (R*x)'*(R*x) = z'*z, if we define z as R*x.
The curve to plot thus becomes such that z'*z=1, and that's a circle. A simple solution is thus z = (cos(t), sin(t)), for 0<=t<=2 pi. You then multiply by the inverse of R to get the ellipse.
This is pretty straightforward to translate into the following code:
function plot_ellipse(E)
% plots an ellipse of the form xEx = 1
R = chol(E);
t = linspace(0, 2*pi, 100); % or any high number to make curve smooth
z = [cos(t); sin(t)];
ellipse = inv(R) * z;
plot(ellipse(1,:), ellipse(2,:))
end
Hope this might help!
Hi I have a set of data A with each element corresponding to an x and y combination. When I plot this dat using mesh I get a graph with many spikes on it. This is not unnexpected but I would like a way to smooth these out to get a smooth surface.
I've tried to use the smooth3 command but cannot figure out how to make the suitable input.
Any help would be appreciated. Thanks
This is how my data is generated.
function v = f(x,y) % Not actual function
return x*rand()+y*rand()
end
x = 0.05:0.01:0.95;
y = 0.05:0.01:0.95;
o = zeros(length(x),length(y));
A = zeros(length(x), length(y));
for k = 1:5
for i = 1:length(x)
for j = 1:length(y)
o(i,j) = f([x(i), y(j)]);
end
end
A= A+o;
end
A = A/5;
This is what produces the plot.
[X,Y] = meshgrid(x);
mesh(A)
my be you can try a convolution of your variable A with a filter(the following is an example of a Gaussian filter).
C = conv2(A,fspecial('gaussian', hsize, sigma));
check conv2 and fspecial in matlab help
Does anyone know how to obtain a mean curve having a matrix with the correspondent x,y points from the original plot? I mean, I pretend a medium single curve.
Any code or just ideas would be very very helpful for me since I am new with matlab.
Thank you very much!
Well, one thing you can do is fit a parametric curve. Here's an example on how to do this for a figure-8 with noise on it:
function findParamFit
clc, clf, hold on
%# some sample data
noise = #(t) 0.25*rand(size(t))-0.125;
x = #(t) cos(t) + noise(t);
y = #(t) sin(2*t) + noise(t);
t = linspace(-100*rand, +100*rand, 1e4);
%# initial data
plot(x(t), y(t), 'b.')
%# find fits
options = optimset(...
'tolfun', 1e-12,...
'tolx', 1e-12);
a = lsqcurvefit(#myFun_x, [1 1], t, x(t), -10,10, options);
b = lsqcurvefit(#myFun_y, [1 2], t, y(t), -10,10, options);
%# fitted curve
xx = myFun_x(a,t);
yy = myFun_y(b,t);
plot(xx, yy, 'r.')
end
function F = myFun_x(a, tt)
F = a(1)*cos(a(2)*tt);
end
function F = myFun_y(b, tt)
F = b(1)*sin(b(2)*tt);
end
Note that this is a particularly bad way to fit parametric curves, as is apparent here by the extreme sensitivity of the solution to the quality of the initial values to lsqcurvefit. Nevertheless, fitting a parametric curve will be the way to go.
There's your google query :)