Error in @(t)(yt.*e​xp(-sqrt(-​1).*omega.​*t));Error in integralCa​lc/iterate​ScalarValu​ed (line 314) fx = FUN(t);

Can someone help me understand what the error in this code is please?
if true
format long
a = 1
b = 3*10.^-7
c = 5*10.^-8
f0 = 4*10.^9
sigma = 0.2
t0 = 0
tmax = 2.*b
f = linspace(10.^6, 10.^10)
omega = 2.*pi.*f
omega0 = 2.*pi.*f0
yt = a.*exp((-(t-b).^2)/((2*c).^2))
figure(1)
plot(t,yt)
fun = @(t) (yt.*exp(-sqrt(-1).*omega.*t))
q = integral(fun,0,6*10^-7)
% code
end
I'm trying to integrate fun and have tried many ways but I still cant get it.

 Akzeptierte Antwort

This runs. You didn’t define ‘t’ in the code you posted, so I created it. Otherwise, I changed your integral call to specify your function to be 'ArrayValued'. I have no idea if it produces the result you want, so experiment with it:
a = 1;
b = 3E-7;
c = 5E-8;
f0 = 4E9;
sigma = 0.2;
t0 = 0;
tmax = 2.*b;
f = linspace(1E6, 1E10);
t = linspace(t0, tmax); % Created To Define ‘t’
omega = 2.*pi.*f;
omega0 = 2.*pi.*f0;
yt = a.*exp((-(t-b).^2)./((2*c).^2));
fun = @(t) (yt.*exp(-1i.*omega.*t));
q = integral(fun,0,6E-7, 'ArrayValued',1);
figure(1)
plot(t,yt)

6 Kommentare

Thank you! I had t defined in an earlier version of my code but thought that was my problem since I had fun defined in terms of f. This helped me get to the result that I was looking for with some additional tweaking, thank you again!
As always, my pleasure!
Another option if you don’t want to define ‘yt’ as a separate vector is to define it as an anonymous function as well:
a = 1;
b = 3E-7;
c = 5E-8;
f0 = 4E9;
sigma = 0.2;
t0 = 0;
tmax = 2.*b;
f = linspace(1E6, 1E10);
t = linspace(t0, tmax); % Created To Define ‘t’
omega = 2.*pi.*f;
omega0 = 2.*pi.*f0;
yt = @(t) a.*exp((-(t-b).^2)./((2*c).^2)); % Create Anonymous Function
fun = @(t) (yt(t).*exp(-1i.*omega.*t));
q = integral(fun,0,6E-7, 'ArrayValued',1);
figure(1)
plot(t,yt(t))
The only other changes required then are to refer to it as a function inside the ‘fun’ definition and plot call. I don’t know if it’s more efficient, but it may be easier.
Can you help me with another integration? I'm trying to integrate the square of what I got out of this prior integration. I have the square and was able to plot it, but I don't think I quite understand how to make the integration work for this second part because I followed what you did for the first integration and it doesn't work out the same way.
if true
format long
a = 1
b = 3*10.^-7
c = 5*10.^-8
f0 = 4*10.^9
sigma = 0.4
t0 = 0
tmax = 2*b
f = linspace(10.^6,10.^10)
t = linspace(t0, tmax)
omega = 2*pi*f
omega0 = 2*pi*f0
yt = a.*exp((-(t-b).^2)./((2*c).^2)) % equation (1)
figure(1)
plot(t,yt) % plot of (1) vs time for t:(0 to 6*10^-7)
fun = @(t) (yt.*exp(-1i.*omega.*t))
q = integral(fun,0,6E-7, 'ArrayValued',1) % equation (2)
figure(2)
plot(f,q) % plot of magnitude of F (eq 2) vs frequency for f:(10^6 to 10^10)
r = q.^2 % square of magnitude of F
figure(3)
plot(f,r) % plot of square of magnitude of F
fun2 = r
s = integral(fun2,3.8E9,43.E9, 'ArrayValued',1) % integral of square of magnitude of F
figure(4)
plot(f,s) % plot of integral of square of magnitude of F
% code
end
I included all of the code needed to understand what I'm talking about. would fun2 not be a proper definition for a function handle?
In your code, ‘fun2’ is not a function handle.
I named the integral of the squared function ‘q2’ in my code, and integrated it this way:
a = 1;
b = 3E-7;
c = 5E-8;
f0 = 4E9;
sigma = 0.2;
t0 = 0;
tmax = 2.*b;
f = linspace(1E6, 1E10);
t = linspace(t0, tmax); % Created To Define ‘t’
omega = 2.*pi.*f;
omega0 = 2.*pi.*f0;
yt = @(t) a.*exp((-(t-b).^2)./((2*c).^2)); % Create Anonymous Function
fun = @(t) (yt(t).*exp(-1i.*omega.*t));
q = integral(fun,0,6E-7, 'ArrayValued',1);
figure(1)
plot(t,yt(t))
q2 = integral(@(t) fun(t).^2, 0,6E-7, 'ArrayValued',1);
figure(2)
plot(t, q2)
grid
I had this code from earlier in the day, and used the anonymous function version of ‘yt’.
Also, with respect to defining ‘f’ (and ‘omega’) and ‘t’, they have to be the same lengths or the code will throw an error.
Oh ok so its not necessary to define the squared function before putting it into the next integral? That's interesting and really helpful! Thank you!
My pleasure!
You have to define the function you want to integrate specifically in the integrand. (Your function is complex, so note that squaring it is not the same as multiplying it by its complex-conjugate, or taking its absolute value.)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Your omega is 1 x 100. For omega.*t to work, your t would have to be either scalar or 1 x 100. But http://www.mathworks.com/help/matlab/ref/integral.html#inputs
For scalar-valued problems, the function y = fun(x) must accept a vector argument, x, and return a vector result, y.
That is, the value passed in (your "t") will be a vector of arbitrary length and you need to return a value for each entry in "t". But your omega is length 100 so which one value do you want to return?
Perhaps you want each integral call to be a single t and that you return one value for each omega. If so then pass 'ArrayValued', 1 to the int() call:
q = integral(fun,0,6*10^-7, 'ArrayValued', 1);
I note, though, that you define yt in terms of t, and you do so at a point that t is not defined. Is the t there intended to be the same t as in fun? If so then you need to define yt as a function and invoke it as a function:
yt = @(t) a.*exp((-(t-b).^2)/((2*c).^2));
fun = @(t) (yt(t).*exp(-sqrt(-1).*omega.*t));
q = integral(fun,0,6*10^-7, 'ArrayValued', 1);
This will return q the same size as omega; the values in q will be complex.

2 Kommentare

Both of you helped me achieve what I wanted to get out of the code, but can you tell me why omega is length 100? Is it because it is multiplied by f which is going to be length 100?
Yes, you have 2*pi*f and f is length 100 so the outcome is length 100

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by