Fourier coefficients using matlab numerical integration - fft

I have been trying to display the an and bn fourier coefficients in matlab but no success, I was able to display the a0 because that is not part of the iteration.
I will highly appreciate your help, below is my code
syms an;
syms n;
syms t;
y = sym(0);
L = 0.0005;
inc = 0.00001; % equally sample space of 100 points
an = int(3*t^2*cos(n*pi*t/L),t,-L,L)*(1/L);
bn = int(3*t^2*sin(n*pi*t/L),t,-L,L)*(1/L);
a0 = int(3*t^2,t,-L,L)*(1/L);
a0 = .5 *a0;
a0=a0
for i=1:5
y = subs(an, n, i)*cos(i*pi*t/0.0005)
z = subs(bn, n, i)*sin(i*pi*t/0.0005)
end

If everything you stated within your question is correct, I would tend to solve it this way:
clc, clear all,close all
L = 0.0005;
n = 5;
an = zeros(1,n);
bn = zeros(1,n);
for i = 1:5
f1 = #(t) 3.*(t.^2).*cos(i.*pi.*t./L);
f2 = #(t) 3.*(t.^2).*sin(i.*pi.*t./L);
an(i) = quad(f1,-L,L).*(1./L);
bn(i) = quad(f2,-L,L).*(1./L);
a0 = .5.*quad(#(t) 3.*t.^2,-L,L).*(1./L);
end
I hope this helps.

Related

Octave changes scale of y axis when I change the x-axis

When I used this code, it plots two functions like this:
a = 2;
t0 = 1;
N = 100;
epsilon = 1e-5;
function t = metodoDeNewton(a, t0, N, epsilon)
t = zeros(1, N+1);
t(1) = t0;
for i = 1:N
t(i+1) = t(i) - (t(i).^2 - (a - sin(t(i)))) ./ (2 .* t(i) - cos(t(i)));
if abs(t(i+1) - t(i)) < epsilon
break;
endif
endfor
endfunction
t = metodoDeNewton(a, t0, N, epsilon);
x = 0:0.01:1;
y1 = t;
y2 = a - sin(t);
l = plot(x, y1, x, y2);
legend({'g(a)', 'h(a)'});
xlabel('a');
ylabel('y');
But, when I try to change the x to x = 0:0.01:0.2 the graph's y-axis scale changes and I'm no longer able to see the functions I believe.
How can I fix this, any help would be appreciated!

Using folve to solve implicit function V.S. Desmos

I have an implicit equation, like this:
(a1 X + b1 Y + m)*(a2 X + b2 Y + m)*(a3 X + b3 Y + m) - c = 0,
a1,a2,b1,b2,a3,b3 are certain value, c is a variant. According different c, I need to solve it to get a set of (x,y), which I will use to integrate.
The listed function I have in practice is much more complex than this, so I am confused as to why when I put this equation into the website desmos to draw this implicit function, I can get the solutions that satisfies this function and I would like to know why this is so fast for desmos and then if there is a better way to find these solutions
I using polar coordinate to solve this problem
c_range = np.linspace(0,c_max, 1000)
theta_range = np.linspace(0,pi, 1000)
for i in range(1000):
if i ==0:
c = c_range[i]
for j in range(1000):
theta = theta_range[j]
r = fsolve(#func, r0, args=(c, theta))
radius[i][j] = r
r0 = r
else:
c = c_range[i]
r0 = radius[i-1]
r = fsolve(#func, r0, args=(c, theta_range))
radius[i]= r

Simulating waves in disc or circle form

I am getting an error when I run this code while selecting disc view or circle view option for wave simulation. The code and error are attached. I think there is some problem in this part of code typically in fzero function. Any help would be great.
Code:
function z = bjzeros(n,k)
% BJZEROS Zeros of the Bessel function.
% z = bjzeros(n,k) is the first k zeros of besselj(n,x)
% delta must be chosen so that the linear search can take
% steps as large as possible
delta = .99*pi;
Jsubn = inline('besselj(n,x)''x','n');
a = n+1;
fa = besselj(n,a);
z = zeros(1,k);
j = 0;
while j < k
b = a + delta;
fb = besselj(n,b);
if sign(fb) ~= sign(fa)
j = j+1;
z(j) = fzerotx(Jsubn,[a b],n);
end
a = b;
fa = fb;
end
Error:
Undefined function 'fzerotx' for input arguments of type 'inline'.
Error in waves>bjzeros (line 292)
z(j) = fzerotx(Jsubn,[a b],n);
Error in waves (line 137)
mu = [bjzeros(0,2) bjzeros(1,2)];
Function Declarations and Syntax
The fzerotx() function may not be declared. You can follow the file structure below to create the required M-files/functions in the same directory. Another small error may be caused by a missing comma, I got rid of the error by changing the line:
Jsubn = inline('besselj(n,x)''x','n');
to
Jsubn = inline('besselj(n,x)','x','n');
File 1: Main File/Function Call → [main.m]
mu = [bjzeros(0,2) bjzeros(1,2)];
File 2: bjzeros() Function → [bjzeros.m]
function z = bjzeros(n,k)
% BJZEROS Zeros of the Bessel function.
% z = bjzeros(n,k) is the first k zeros of besselj(n,x)
% delta must be chosen so that the linear search can take
% steps as large as possible
delta = .99*pi;
Jsubn = inline('besselj(n,x)','x','n');
a = n+1;
fa = besselj(n,a);
z = zeros(1,k);
j = 0;
while j < k
b = a + delta;
fb = besselj(n,b);
if sign(fb) ~= sign(fa)
j = j+1;
z(j) = fzerotx(Jsubn,[a b],n);
end
a = b;
fa = fb;
end
end
File 3: fzerotx() Function → [fzerotx.m]
Function Reference: MATLAB: Textbook version of FZERO
function b = fzerotx(F,ab,varargin)
%FZEROTX Textbook version of FZERO.
% x = fzerotx(F,[a,b]) tries to find a zero of F(x) between a and b.
% F(a) and F(b) must have opposite signs. fzerotx returns one
% end point of a small subinterval of [a,b] where F changes sign.
% Arguments beyond the first two, fzerotx(F,[a,b],p1,p2,...),
% are passed on, F(x,p1,p2,..).
%
% Examples:
% fzerotx(#sin,[1,4])
% F = #(x) sin(x); fzerotx(F,[1,4])
% Copyright 2014 Cleve Moler
% Copyright 2014 The MathWorks, Inc.
% Initialize.
a = ab(1);
b = ab(2);
fa = F(a,varargin{:});
fb = F(b,varargin{:});
if sign(fa) == sign(fb)
error('Function must change sign on the interval')
end
c = a;
fc = fa;
d = b - c;
e = d;
% Main loop, exit from middle of the loop
while fb ~= 0
% The three current points, a, b, and c, satisfy:
% f(x) changes sign between a and b.
% abs(f(b)) <= abs(f(a)).
% c = previous b, so c might = a.
% The next point is chosen from
% Bisection point, (a+b)/2.
% Secant point determined by b and c.
% Inverse quadratic interpolation point determined
% by a, b, and c if they are distinct.
if sign(fa) == sign(fb)
a = c; fa = fc;
d = b - c; e = d;
end
if abs(fa) < abs(fb)
c = b; b = a; a = c;
fc = fb; fb = fa; fa = fc;
end
% Convergence test and possible exit
m = 0.5*(a - b);
tol = 2.0*eps*max(abs(b),1.0);
if (abs(m) <= tol) | (fb == 0.0)
break
end
% Choose bisection or interpolation
if (abs(e) < tol) | (abs(fc) <= abs(fb))
% Bisection
d = m;
e = m;
else
% Interpolation
s = fb/fc;
if (a == c)
% Linear interpolation (secant)
p = 2.0*m*s;
q = 1.0 - s;
else
% Inverse quadratic interpolation
q = fc/fa;
r = fb/fa;
p = s*(2.0*m*q*(q - r) - (b - c)*(r - 1.0));
q = (q - 1.0)*(r - 1.0)*(s - 1.0);
end;
if p > 0, q = -q; else p = -p; end;
% Is interpolated point acceptable
if (2.0*p < 3.0*m*q - abs(tol*q)) & (p < abs(0.5*e*q))
e = d;
d = p/q;
else
d = m;
e = m;
end;
end
% Next point
c = b;
fc = fb;
if abs(d) > tol
b = b + d;
else
b = b - sign(b-a)*tol;
end
fb = F(b,varargin{:});
end
Ran using MATLAB R2019b

How can I save output arguments' function generated by a loop in a matrix using scilab?

(Scilab)
I'm using a function in a loop. I'd like to save every output argument in a matrix. I can display it but I want to save it.
for x = 0:loopduration:(Endtrial-120);
y = x + 120;
Deb = x;
Fin = y;
Moy = Data_Moy(Data, Deb, Fin);
disp(Moy);
end;
If Data_Moy yields a matrix with fixed sized among iterations I would use a tensor/3D matrix like this:
k = 1;
for x = 0:loopduration:(Endtrial-120);
y = x + 120;
Deb = x;
Fin = y;
Moy(:,:,k) = Data_Moy(Data, Deb, Fin);
k = k+1;
end;
then you can later display or do anything you want with the submatrices e.g
disp(Moy(:,:,1))

Surface plot of equally spaced grid of overlapping Gaussians not plotting correctly?

I am attempting to make a surface plot in Octave based on a bunch of overlapping, offset curves. the function should be treating x and y identically but I am not seeing that in the final plot, rather it has ridges running along one axis. I cant tell if this is a plotting error or something else wrong with my code. am hoping you can provide some help/insight.
Plot with ridges following one axis
Thanks
clear;
graphics_toolkit gnuplot
V = 2500; %scan speed in mm
rr = 200000; %rep rate in Hz
Qsp = 1; %pulse energy
pi = 3.14159;
ns = 1; %scan number
w = 0.0125; %Gaussian radius in mm
dp = 0.0125; %lateral pulse distance in mm
dh = 0.0125; %hatch pitch in mm
xmax = 0.2; %ablation area x in mm
ymax = 0.2; %ablation area y in mm
np = round(xmax/dp);
nh = round(ymax/dh);
points = 50;
cof = ns*2*Qsp/(pi*w^2);
N = [0:1:np];
M = rot90([0:1:nh]);
for i = 1:points
for j = 1:points
x = (i-1)*xmax/(points-1);
y = (j-1)*ymax/(points-1);
k = exp(-(2*(x-N*dp).^2+(y-M*dh).^2)/(w^2));
H(i,j) = cof*sum(sum(k));
endfor
endfor
ii = [1:1:points];
jj = ii;
xx = (ii-1)*xmax/(points-1);
yy = (jj-1)*ymax/(points-1);
surf(xx,yy,H);
I find it helpful to space out components of complex expressions.
k = exp(-(2*(x-N*dp).^2+(y-M*dh).^2)/(w^2));
It's too hard to read with so many parentheses.
k = exp(
-(
2*(x-N*dp).^2 + (y-M*dh).^2
) / (w^2)
);
Do you see it already?
k = exp(
-(
2 * (x-N*dp).^2
+
(y-M*dh).^2
) / (w^2)
);
The x component gets multiplied by two, but the y component doesn't.