Matlab cart2pol function - matlab

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.

Related

fplot not plotting an exponential function

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.

Plot symbolic derivative of a built-in MATLAB function

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

How to perform indefinite integration of this function in MATLAB?

I need to perform the following operations as shown in the image. I need to calculate the value of function H for different inputs(x) using MATLAB.
I am giving the following command from Symbolic Math Toolbox
syms y t x;
f1=(1-exp(-y))/y;
f2=-t+3*int(f1,[0,t]);
f3=exp(f2);
H=int(f3,[0,x]);
but the value of 2nd integral i.e. integral in the function H can't be calculated and my output is of the form of
H =
int(exp(3*eulergamma - t - 3*ei(-t) + 3*log(t)), t, 0, x)
If any of you guys know how to evaluate this or have a different idea about this, please share it with me.
Directly Finding the Numerical Solution using integral:
Since you want to calculate H for different values of x, so instead of analytical solution, you can go for numerical solution.
Code:
syms y t;
f1=(1-exp(-y))/y; f2=-t+3*int(f1,[0,t]); f3=exp(f2);
H=integral(matlabFunction(f3),0,100) % Result of integration when x=100
Output:
H =
37.9044
Finding the Approximate Analytical Solution using Monte-Carlo Integration:
It probably is an "Elliptic Integral" and cannot be expressed in terms of elementary functions. However, you can find an approximate analytical solution using "Monte-Carlo Integration" according to which:
where f(c) = 1/n Σ f(xᵢ)
Code:
syms x y t;
f1=(1-exp(-y))/y; f2=-t+3*int(f1,[0,t]); f3=exp(f2);
f3a= matlabFunction(f3); % Converting to function handle
n = 1000;
t = x*rand(n,1); % Generating random numbers within the limits (0,x)
MCint(x) = x * mean(f3a(t)); % Integration
H= double(MCint(100)) % Result of integration when x=100
Output:
H =
35.2900
% Output will be different each time you execute it since it is based
% on generation of random numbers
Drawbacks of this approach:
Solution is not exact but approximated.
Greater the value of n, better the result and slower the code execution speed.
Read the documentation of matlabFunction, integral, Random Numbers Within a Specific Range, mean and double for further understanding of the code(s).

Using fzero in Matlab or Octave, avoiding for loop and complex solutions

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.

parametric integration and numeric plotting of a 2D function

I am going to devise a 2D function as a probability density function, is which a function of two variables, i.e. f = f(x,n). Then, as the target is plotting the probability variation, the integration in related to parameter x should be taken into account. The t parameter is the variable planned to be the upper bound of the integration. It is suffice to say that n is the other tuning factor. Finally, with due attention to the considered meshgrid, the probability surface is supposed to be drawn.
My option for the integration process is the symbolic int function. But there is an error: Error using mupadmex
Here is my code:
clear;
syms x;
syms n;
syms t;
sigma = 1;
mu = 0;
[t,n] = meshgrid(0:0.01:20, 1:1:100);
f = (n./(2*sigma*sqrt(pi))).*exp(-((n.*x)./(2.*sigma)).^2);
ff = int(f, x, -inf, t);
mesh(n,t,ff);
And the error trace:
Error using mupadmex
Error in MuPAD command: The argument is invalid. [Dom::Interval::new]
Error in sym/int (line 153)
rSym = mupadmex('symobj::intdef',f.s,x.s,a.s,b.s,options);
Error in field (line 14)
ff = int(f, x, -inf, t);
Would you please helping me to overcome this tie?!
PS. I know that there are some ideas to do this stuff more numerically by integral function, but I am prone to handle this case by int function, if it is possible. Because this code should be used as a service by the other snippets and the generated ff parameter is completely deserving, however it won't be a closed form function.
Thanks in advance.
I have changed several details in your code, Ill try to explain all of them.
No need of defining symbolic variables that are not going to be symbolic.
code:
clear;
syms x;
sigma = 1;
mu = 0; % This is never used!
You want to integrate a function f(n,x) dx for different n. If you create a meshgrid of n (instead of a vector), you will have tons of repeated f(ni,x) that you are not going to use.
Just do:
t=(0:1:20);
n=(1:10:100);
% are you sure you dont want ((n.*x)-mu) here?
f = (n./(2*sigma*sqrt(pi))).*exp(-((n.*x)./(2.*sigma)).^2);
int does not accept a mesh of symbolic functions! (or I haven't managed to make it work...).
So put a couple of for's there!
for ii=1:length(n)
for jj=1:length(t)
ff(ii,jj) = int(f(ii), x, -inf, t(jj));
end
end
Now we DO want a meshgrid for the plot!
Like this:
[t,n] = meshgrid(t, n);
And you want to plot the numerical value, so use double() to convert from symbolic to numerical:
plot!:
mesh(n,t,double(ff));
Result (with low amount of points due to the obvious computational effort needed)