I'm trying to plot this function 0.5*sinc(n/2)*e^(0.5*j*n*pi*t) using this code:
n = 1;
x2t = #(t) 0.5*sinc(n/2)* exp(sqrt(-1)*n*pi*0.5*t);
fplot(x2t);
but I only get blank results, what's the problem?
Your function produces a complex result, and fplot does not plot that properly. Instead, you could plot the real and imaginary components separately:
n = 1;
x2t = #(t) 0.5*sin(n/2)/(n/2) * exp(1i*n*pi*0.5*t);
fplot(#(t)real(x2t(t)));
hold on
fplot(#(t)imag(x2t(t)));
Notice that I replaced sinc(n/2) with sin(n/2)/(n/2), since I don't have the sinc function in my version of MATLAB. I also replaced sqrt(-1) with the simpler 1i.
Related
dx = 0.1
x = 0:dx:30:
for x<5, f(x)= 0.1*(x^4)-5.6*(x^3)+3.6*(x^2)–16.2*(x^3)+6.2*(x^2)+11.5*(x)-9.8
for 5<=x<20, f(x)= 0.4*(x^3)+1.2(x^2)+9.7*(x)–89.6
for 20<=x, f(x)= 0.8*(x^2)+8.9*(x)-8.2
I have tried using a for loop with if and elseif statements but cant make it work, if someone could help that would be great
A piecewise function that uses a symbolic variable syms x might suffice in this case. The piecewise function allows you to plot multiple functions over only a distinct bounds/regions. It is also possible with anonymous functions and different x vectors as input.
syms x
f1=0.1*(x^4)-5.6*(x^3)+3.6*(x^2)-16.2*(x^3)+6.2*(x^2)+11.5*(x)-9.8;
f2=0.4*(x^3)+1.2*(x^2)+9.7*(x)-89.6;
f3=0.8*(x^2)+8.9*(x)-8.2;
Bounds_1 = x<5;
Bounds_2 = 5<=x<20;
Bounds_3 = 20<=x;
y = piecewise(Bounds_1,f1,Bounds_2,f2,Bounds_3,f3);
fplot(y);
xlim([-5 50]);
Does the Matlab's cart2pol() function calculate the value of theta differently? Here's an example i worked out when converting to polar coordinates using cart2pol.
First, i implement it with cart2pol.
N = 8;
x = 1:N; y = x;
[X,Y] = meshgrid(x,y);
[theta,rho] = cart2pol(X-floor(N/2),-(Y-floor(N/2)))
which gives
Now, if i use the equation which is:
Note in Matlab, -1 and +1 is not required
theta = atan2((N-2.*Y),(2.*X-N))
i get:
Why is it that the value 3.1416 is negative for the cart2pol function and positive based on the equation?
As beaker said, the angle is the same. But regarding your comment: You can always use edit <functionname> to see how the Matlab function is implemented. This usually works for .m files, but not for P and MEX files, since these are binary and therefore can't be edited directly. So
edit cart2pol
in this case and you'll see that all cart2poldoes, is just atan2(y,x) to get theta. So this means that the different results are just due to the fact that you call the function using different inputs than the inputs you use in your "formula".
atan2(-(Y-floor(N/2)),X-floor(N/2))
gives exactly the same result as your call of cart2pol.
I am trying to compute the second derivative of the airy function. Only its first derivative is a predefined function in MATLAB (airy(1,x))
Is there a way to compute its symbolic derivative? without resorting to finite differences, etc
I tried this
syms x
aiprime = #(x) airy(1,x);
aisecond = diff(airy(1,x));
plot(-10:0.01:10,aiprime,aisecond)
But didn't work.
Error using plot
Invalid second data argument
The problem is your plot statement. You specify the desired x-data, but did not evaluate your function in these points:
syms x
aiprime(x) = airy(1,x); % I would define it as a symbolic function instead of a function handle, although it works too.
aisecond(x) = diff(airy(1,x)); % Define as a function, to be able to evaluate the function easily
xs = -10:0.01:10; % define your desired x points
plot(xs,aiprime(xs),xs, aisecond(xs)) % evaluate your functions and plot data
I'm using fzero function to solve a non-linear equation depending on one parameter
and I'm not satisfied with my method. I have these issues:
1) Can for loop for the parameter be avoided ?
2) In order to avoid complex solutions I first have to pre-compute valid interval for fzero.
Is there is a better solution here ?
If I reduce the parameter step size the execution time becomes slow. If I don’t pre-compute
the interval I get an error "Function values at interval endpoints must be finite and real."
in Matlab and "fzero: not a valid initial bracketing" in Octave.
Here is the code
% solve y = 90-asind(n*(sind(90-asind(sind(a0)/n)-y)))
% set the equation paramaters
n=1.48; a0=0:0.1:60;
% loop over a0
for i = 1:size(a0,2)
% for each a0 find where the argument of outer asind()
% will not give complex solutions, i.e. argument is between 1 and -1
fun1 = #(y) n*(sind(90-asind(sind(a0(i))/n)-y))-0.999;
y1 = fzero(fun1,[0 90]);
fun2 = #(y) n*(sind(90-asind(sind(a0(i))/n)-y))+0.999;
y2 = fzero(fun2,[0 90]);
% use y1, y2 as limits in fzero interval
fun3 = #(y) 90-asind(n*(sind(90-asind(sind(a0(i))/n)-y)))-y;
y(i) = fzero(fun3, [y1 y2]);
end
% plot the result
figure; plot(y); grid minor;
xlabel('Incident ray angle [Deg]');
ylabel('Lens surface tangent angle');
With Matlab, I obtained the plot below with the following simplified loop.
for i = 1:size(a0,2)
fun3 = #(y) sind(90-y) - n*(sind(90-asind(sind(a0(i))/n)-y));
y(i) = fzero(fun3, [0,90]);
end
The difference is in the form of equation: I replaced 90-y = asind(something) with sin(90-y) = something. When "something" is greater than 1 in absolute value, the former version throws an error due to complex value of asind. The latter proceeds normally, recognizing that this is not a solution (sin(90-y) can't be equal to something that is greater than 1).
No precomputing of the bracket was necessary, [0,90] simply worked. Another change I made was in the plot: plot(a0,y) instead of plot(y), to get the correct horizontal axis.
And you can't avoid for loop here, nor should you worry about it. Vectorization means eliminating loops where the content is a low-level operation that can be done en masse by operating on some C array. But fzero is totally not that. If the code takes long to run, it's because solving a bunch of equations takes long, not because there's a for loop.
I'm totally new to MATLAB and I know only few basic commands. My task is to plot a function of this kind:
I(T) = ((2*J(k*r*sin(T))/(k*r*sin(T))))^2
where
T = angle
k = (2*pi*f)/c (f= frequency in Hz and c is speed of sound)
r = radius
J = bessel function first kind
I explain a bit: the function represent the power of a soundwave in the space. I've tried many times to plot this but i get always a single point in the plot.
I assume you've defined your Bessel function in J. If not, the MATLAB command for a Bessel function of the first kind is besselj. You'll also have to specify the order of the Bessel function.
You can define your anonymous function as
f=#(t,k,r)(2*besselj(0,k*r*sin(t))./(k*r*sin(t))).^2
and plot it as
T=linspace(0,pi,100);%# a sample angle vector
plot(T,f(T,k,r)) %# where k and r are values you'll have to provide
what about
I = ((2*J(k*r*sin(T))./(k*r*sin(T)))).^2
I finally found how to manage the problem described above, here's the solution that i found, in case that other people may need it.
% MATLAB Istruction for graph generation of bessel like function %
% Variables (fixed values)%
k = 912.91
r = 0.0215
% Set the range for the angle theta %
theta = (-(2/3)*pi:pi/180:(2/3)*pi)
% Calculation of bessel function of first kind %
J = besselj(1,k*r*sin(theta))
% Calculation I function %
% notice the ./ and .^ operators
I = ((2*J)./(k*r*sin(theta))).^2
% now plot the results with the plot command
plot(theta,I)