Why is my trigonometric interpolation not working? - matlab

I am working on finding the trigonometric interpolation for 8 evenly spaced points on the interval [-1, 1]. The data points are given by (t_j, f(t_j)) where f(t_j) = e^(t_j).
Here is my code using matlab,
f = #(t) exp(t);
n = 8;
tim = ones(1, 8);
X = ones(1,8);
for j = 1:n
tim(j) = -1 + (j-1)*(2/n);
X(j) = f(tim(j));
end
Y = fft(X');
a = real(Y);
b = imag(Y);
P = #(t) a(1)/sqrt(n) + (2/sqrt(n))*((a(2)*cos(pi*(t+1))-b(2)*sin(pi*(t+1)))+(a(3)*cos(2*pi*(t+1))-b(3)*sin(2*pi*(t+1)))+(a(4)*cos(3*pi*(t+1))-b(4)*sin(3*pi*(t+1)))) + (a(5)/sqrt(n))*cos(n*pi*(t+1)/2);
hold on
fplot(P, [-1, 1]);
fplot(f, [-1, 1]);
Output of code
The interpolation doesn't even hit any of the data points. I don't really have any clue where I've gone wrong.

Can't it be that you are off by a scaling factor ?

Related

MATLAB plotting trajectory

I would like to plot the angular velocity of point B by using MATLAB. However, there is some mistake in my code for angular velocity that I couldn't fix.
The length of the input link OA of the mechanism is r = 50 mm, length of AB is l = 150 mm. Fixed coordinates of point C are xC = d = 80 mm and yC = 0 mm.
Angular velocity of OA is ω = 15 rad/s.
%Full trajectory of B
%Linkage dimensions
clear
r = 50;
l = 150;
xC = 80;
yC = 0;
n = 361; % Number of position calculations
fii = linspace(0,2*pi,n);
omega = 15;
[xA,yA] = pol2cart(fii,r);
d = xA + xC;
alpha = atan(yA./(xC-xA));
lx = l*cos(alpha);
ly = l*sin(alpha);
xB = xA + lx;
yB = yA + ly;
plot(xB,yB) %Plots the trajectory
title('Full trajectory of AB')
% Angular velocity of AB
for ind = 1:n
omegaAB(ind) = (-(r^2-d*r*cos(fii))/(r^2 + d^2 - 2*d*r*cos(fii)))*omega;
end
figure(2)
plot(fii,omegaAB, 'linewidth', 2, 'color', 'red')
title('Angular velocity of AB')
ylabel('\omega_{AB} [1/s]')
xlabel('\phi [rad]')
I think there is just one mistake in your code when you compute alpha:
alpha = atan(yA./(xC-xA));
This gives the following trajectory:
Your code is partially vectorized but you're still looping over ind which is why you are getting matrix dimensions errors. You can either remove the loop and make it completely vectorized or you can make sure that all of your vectors with length n are indexed properly:
for ind = 1:n
omegaAB(ind) = (-(r^2-d*r*cos(fii(ind)))/(r^2 + d(ind)^2 - 2*d*r*cos(fii(ind))))*omega;
end

Plotting linear hyperplane using primal vector in Matlab

Using svmtrain and svmmodel, we're supposed to plot a hyperplane to separate two collections of data. Through the sample, I have the data:
c = [1 1; 2 1.5; 2 1;3 1.5];
N = 10; X = []; sigma = 0.2;
for i = 1:4
X = [X; sigma*randn(N,2) + repmat(c(i,:),N,1)];
end
Y = [ones(1,2*N) -ones(1,2*N)];
plot(X(1:end/2,1),X(1:end/2,2),'+')
hold all
plot(X(end/2+1:end,1),X(end/2+1:end,2),'o')
hold on
So, from my understanding, I have to use the primal vector to obtain the hyperplane as: y = w'x + b, which is the following:
model = svmtrain(Y',X,'-s 0 -t 0 -c 1')
w = model.SVs' * model.sv_coef;
b = -model.rho;
if model.Label(1) == -1
w = -w;
b = -b;
end
However, when I add
plot(w+b)
I get:
Which is not the desired hyperplane. I've played with the values as much as I can and looked up different samples of this kind, but I can't figure out how I'm supposed to use the primal and bias to produce the proper hyperplane.

Matlab piecewise function in same plot as continuous

I'm trying to plot a piecewise function as an interpolation for the function f(x) = 1/(1+25x^2). This is how I plotted two functions previously when I wasn't dealing with piecewise.
z = linspace(-1,1,200);
yexact = 1./(1+25.*z.^2);
plot(z,yexact)
N=2;
x = size(N+1);
for i = 1:(N+1)
x(i) = -1+(1+cos(((2*i+1)*pi)/(2*(N+1))));
end
a = polyfit(x,1./(1+25.*x.^2),N);
yinter = polyval(a,z);
plot(z,yexact,z,yinter);
title('N = 2');
legend('exact','interpolation');
This was done for N = 2, 5, 10, 15, 20, 30. Now I need to change this to work for piecewise with the same N values. The x(i)'s are the intervals and the P(i)'s are the slopes of the piecewise function. So for N = 2, I need to plot P(1) from x(1) to x(2) and P(2) from x(2) to x(3).
N=2;
x = size(N+1);
P = size(N);
for i = 1:(N+1)
x(i) = -1 + 2*i/N;
end
for i = 1:N
P(i) = (1/(1+25*(x(i)).^2)) + ((i-1-x(i))/(x(i+1)-x(i)))*((1/(1+25*(x(i+1)).^2))-(1/(1+25*(x(i)).^2)));
end
All you have to do is to define your N values as a vector and, then, iterate over it. Into each iteration, the result returned by the computation over the current N value is plotted over the existing axis (for more information, visit this link of the official Matlab documentation).
Here is an example:
z = linspace(-1,1,200);
yexact = 1./(1+25.*z.^2);
plot(z,yexact);
hold on;
for n = [2 5 10]
x = size(n+1);
for i = 1:(n+1)
x(i) = -1+(1+cos(((2*i+1)*pi)/(2*(n+1))));
end
a = polyfit(x,1./(1+25.*x.^2),n);
yinter = polyval(a,z);
plot(z,yinter);
end
hold off;
And here is the resulting output:

Plot solution of second order equation in MATLAB

Could you please help me with the following question:
I want to solve a second order equation with two unknowns and use the results to plot an ellipse.
Here is my function:
fun = #(x) [x(1) x(2)]*V*[x(1) x(2)]'-c
V is 2x2 symmetric matrix, c is a positive constant and there are two unknowns, x1 and x2.
If I solve the equation using fsolve, I notice that the solution is very sensitive to the initial values
fsolve(fun, [1 1])
Is it possible to get the solution to this equation without providing an exact starting value, but rather a range? For example, I would like to see the possible combinations for x1, x2 \in (-4,4)
Using ezplot I obtain the desired graphical output, but not the solution of the equation.
fh= #(x1,x2) [x1 x2]*V*[x1 x2]'-c;
ezplot(fh)
axis equal
Is there a way to have both?
Thanks a lot!
you can take the XData and YData from ezplot:
c = rand;
V = rand(2);
V = V + V';
fh= #(x1,x2) [x1 x2]*V*[x1 x2]'-c;
h = ezplot(fh,[-4,4,-4,4]); % plot in range
axis equal
fun = #(x) [x(1) x(2)]*V*[x(1) x(2)]'-c;
X = fsolve(fun, [1 1]); % specific solution
hold on;
plot(x(1),x(2),'or');
% possible solutions in range
x1 = h.XData;
x2 = h.YData;
or you can use vector input to fsolve:
c = rand;
V = rand(2);
V = V + V';
x1 = linspace(-4,4,100)';
fun2 = #(x2) sum(([x1 x2]*V).*[x1 x2],2)-c;
x2 = fsolve(fun2, ones(size(x1)));
% remove invalid values
tol = 1e-2;
x2(abs(fun2(x2)) > tol) = nan;
plot(x1,x2,'.b')
However, the easiest and most straight forward approach is to rearrange the ellipse matrix form in a quadratic equation form:
k = rand;
V = rand(2);
V = V + V';
a = V(1,1);
b = V(1,2);
c = V(2,2);
% rearange terms in the form of quadratic equation:
% a*x1^2 + (2*b*x2)*x1 + (c*x2^2) = k;
% a*x1^2 + (2*b*x2)*x1 + (c*x2^2 - k) = 0;
x2 = linspace(-4,4,1000);
A = a;
B = (2*b*x2);
C = (c*x2.^2 - k);
% solve regular quadratic equation
dicriminant = B.^2 - 4*A.*C;
x1_1 = (-B - sqrt(dicriminant))./(2*A);
x1_2 = (-B + sqrt(dicriminant))./(2*A);
x1_1(dicriminant < 0) = nan;
x1_2(dicriminant < 0) = nan;
% plot
plot(x1_1,x2,'.b')
hold on
plot(x1_2,x2,'.g')
hold off

Matlab: how to write a vector of a few points as a continuous function?

I have two vectors:
x = [0; 1; 2]
y = [2.0000; 0; -14.7781]
If I will plot x and y I will see three points on the xy-plane. But I want to connect those three points and get them as a continuous function:
y = f(x),
y(0) = 2;
y(1) = 0;
y(2) = -14.7781;
y(0.5) = value between 2 and 0.
For example y can be treated as a ZOH (zero order held) continuous signal.
I saw that MATLAB has a function called d2c, which converts a model from discrete to continuous time. But have no idea how to link it with the vector I have already. How to do
this with MATLAB?
OK, your latest edit improves the situation a lot.
However, you still do not demarcate the problem sufficiently.
ZOH would be as simple as
>> x = [0; 1; 2];
>> y = [2.0000; 0; -14.7781];
>> f = #(new_x) y(find(x <= new_x, 1, 'last'));
>> f(0.5)
ans =
2
However, this is not what I think you mean, as the y(0.5) = value between 2 and 0 part of your question indicates.
Perhaps you want a linearly interpolated value:
>> f = #(new_x) interp1(x,y, new_x);
>> f(0.5)
ans =
1
Or a cubic splines interpolation:
>> f = #(new_x) interp1(x,y, new_x, 'spline');
>> f(0.5)
ans =
2.5973
What I'm asking is: what model best describes your signal when the sample time would decrease to infinitesmal values?
An nth degree polynomial can have at most n-1 turning points. Thus, we can do a polynomial regression:
% Input data
yy = [2.0000; 0; -14.7781];
% Parameters
n = length(yy)-1;
x = (0:1:n).';
% Regression
p = polyfit(x,yy,n);
% Plot
f = polyval(p,x);
figure
plot(x,yy,'o',x,f,'-')