please do you mind looking at the code provided and make some adjustment to answ
ID: 2248089 • Letter: P
Question
please do you mind looking at the code provided and make some adjustment to answer the question below? thanks
Explanation / Answer
Script:
T = 1;
ft = 9;
c0 = ft/pi;
t = -2:4/10000:2;
w0 = 2*pi/T;
%N= input ('Number of Harmonics');
N = 3;
fN = zeros(1,length(t));
fN= fN + ft/2;
for k=1:N
fN = fN - c0/k*sinc(k*w0*t);
end
plot(t,fN);
N= 9;
fN = zeros(1,length(t));
fN= fN + ft/2;
for k=1:N
fN = fN - c0/k*sinc(k*w0*t);
end
plot(t,fN);
N= 10;
fN = zeros(1,length(t));
fN= fN + ft/2;
for k=1:N
fN = fN - c0/k*sinc(k*w0*t);
end
plot(t,fN);
N= 1000;
fN = zeros(1,length(t));
fN= fN + ft/2;
for k=1:N
fN = fN - c0/k*sinc(k*w0*t);
end
plot(t,fN);
N= 9;
fN = zeros(1,length(t));
fN= fN + ft/2;
for k=1:N
fN = fN - c0/k*sinc(k*w0*t);
end
plot(t,fN);
Function:
function [a,b,yfit] = Fseries(x,y,n,scale,sincos)
if nargin<3
error('MATLAB:Fseries:MissingInputs','Required inputs are x, y, and n')
end
checkinputs();
% scale x to [-pi,pi]
if scale
x1 = min(x);
x2 = max(x);
x = pi*(2*(x-x1)/(x2-x1) - 1);
end
% make design matrix
nx = x*(1:n);
if isequal(sincos,'b')
F = [0.5*ones(size(x)),cos(nx),sin(nx)];
elseif isequal(sincos,'c')
F = [0.5*ones(size(x)),cos(nx)];
else
F = sin(nx);
end
% do fit
c = Fy;
% extract coefficients
if isequal(sincos,'b')
a = c(1:n+1);
b = c(n+2:end);
elseif isequal(sincos,'c')
a = c;
b = zeros(n,1);
else
a = zeros(n+1,1);
b = c;
end
% evaluate fit
yfit = F*c;
% transpose yfit back to a row, if y was a row
if xrow
yfit = yfit';
end
function checkinputs
% x & y values
if isnumeric(x) && isvector(x) && isnumeric(y) && isvector(y)
if ~isequal(size(x),size(y))
throwAsCaller(MException('MATLAB:Fseries:UnequalData','x and y must be same size'))
end
% transpose x & y to columns if they are rows
if size(x,2)>1
x = x';
y = y';
xrow = true;
else
xrow = false;
end
% remove anything not finite and real
idx = ~(isfinite(x) & isfinite(y) & isreal(x) & isreal(y));
x(idx) = [];
y(idx) = [];
if isempty(x)
throwAsCaller(MException('MATLAB:Fseries:NoData','x and y contain no real, finite data'))
end
else
throwAsCaller(MException('MATLAB:Fseries:WrongDataType','x and y values must be numeric vectors'))
end
% number of terms
if isscalar(n) && isnumeric(n)
if n<0
throwAsCaller(MException('MATLAB:Fseries:NegativeTerms','Number of terms (n) cannot be negative!'));
end
else
throwAsCaller(MException('MATLAB:Fseries:WrongDataType','n must be a numeric scalar'))
end
% optional scaling argument
if exist('scale','var')
if ~islogical(scale)
throwAsCaller(MException('MATLAB:Fseries:WrongDataType','Scaling parameter must be logical (true/false)'))
end
else
scale = true;
end
% optional type argument
if exist('sincos','var')
if ischar(sincos)
sincos = lower(sincos);
if ismember(sincos,{'s','sin','sine'})
sincos = 's';
elseif ismember(sincos,{'c','cos','cosine'})
sincos = 'c';
else
throwAsCaller(MException('MATLAB:Fseries:WrongFourierType',['Unknown type: ',sincos]))
end
else
throwAsCaller(MException('MATLAB:Fseries:WrongDataType','Type must be string (''sin'' or ''cos'')'))
end
else
sincos = 'b';
end
end
end
Tri_pulse:
T = 1;
Vm = 1;
t = 0 : 1/1e3 : pi*T; % 1 kHz sample freq for 1 s
d = 0 : T : 3*T; % 3 Hz repetition frequency
y = Vm.*pulstran(t,d,'tripuls',2*T,1) - 0.5;
[a,b,c ]=Fseries(t,y,10)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.