how to convert vectore to function before integral

im working on a project and the whole time i worked with vectors that shows the numbers of function, but now i want to integral the function, and i cant integral the vector, i cant use trapez, it give whrong unswear too.
%% Q1part1
T0=20;
T1=5;
w0=(2*pi)/T0;
x= exp(-1*abs(t));
for n=1:601
if n>100
x(n)=0;
end
end
for n=1:601
if n>200
x(n)=x(n-200);
end
end
figure(1)
plot(t,x)
xlim([-5 55])
%% Q1part3
fun = @(x,n) exp(-abs(x)-1i*x.*n.*w0);
c=[1:1:81];
t=[-40:1:40];
helper=[1:1:81];
for n=1:81
c(n) = 0;
helper(n) = 0;
helper(n) = integral(@(x) fun(x,n-41),-5,5);
helper(n)=helper(n)./20;
if abs(imag(helper(n)))<0.01
c(n)= real(helper(n));
end
end
figure(2)
plot(t,c)
%% Q1part6
figure(345345)
plot(t,x)
function [EN] = integralfor6(n,x,c)
t=linspace(-5,55,601);
x_n=0;
T0=20;
w0=(2*pi)/T0;
% X=@(t) exp(-2*abs(t))
x=x.*x
figure(8888)
plot(t,x)
for i=-n:n
x_n = x_n+(c(i+40)*(cos(i*w0*t)));
end
end

2 commentaires

darova
darova le 23 Mai 2021
I don't see any trapz funciton. Can you show it?
im not allowed to use it

Connectez-vous pour commenter.

Réponses (1)

Your code contains a few potential errs that have been corrected.
%% Q1part1
T0=20;
T1=5;
t=linspace(-5, 55, 601);
w0=(2*pi)/T0;
x= exp(-1*abs(t));
for n=1:601
if n>100
x(n)=0;
end
end
for n=1:601
if n>200
x(n)=x(n-200);
end
end
figure(1)
plot(t,x)
xlim([-5 55])
%% Q1part3
fun = @(x,n) exp(-abs(x)-1i*x.*n.*w0);
c=1:81;
t=-40:40;
helper=1:81;
for n=1:81
c(n) = 0;
h(n) = 0;
% helper(n) = 0;
helper(n) = integral(@(x) fun(x,n-41),-5,5);
helper(n)=helper(n)./20;
H(n) = quad(@(x) fun(x,n-41),-5,5); % Quad is also tested
if abs(imag(helper(n)))<0.01
c(n)= real(helper(n));
end
if abs(imag(H(n)))<0.01
h(n)= real(H(n));
end
end
figure(2)
plot(t,c, 'r'), title('Integral @')
figure(3)
plot(t, h, 'b'), title('Quad @')
%% Q1part6
[X,t] = integralfor6(n,x,c);
figure(4)
plot(t,X)
function [x_n,t] = integralfor6(n,x,c)
t=linspace(-5,55,601);
x_n=0;
T0=20;
w0=(2*pi)/T0;
% X=@(t) exp(-2*abs(t))
x=x.*x;
figure(5)
plot(t,x)
for i=1:n-40
x_n = x_n+(c(i)*(cos(i*w0*t)));
end
end
Note that quad() is also tested.
Good luck.

Catégories

En savoir plus sur Numerical Integration and Differential Equations dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by