Plotting complex function in MATLAB (polar)? - matlab

I have tried to plot this function:
t=linspace(0,2*pi,100);
a=input('a= ');
b=input('b= ');
c=input('c= ');
k = a*(1-(sin(3*t)).^(2*b))+c;
polar(t,k)
% a=2.6
% b=0.4
% c=5
Each time, I get the following message:
Warning: Imaginary parts of complex X and/or Y arguments ignored.
I have tried the pol2cart method as such:
t=linspace(0,2*pi,100);
a=input('a= ');
b=input('b= ');
c=input('c= ');
k = a*(1-(sin(3*t)).^(2*b))+c;
[x,y] = pol2cart(t,k);
plot(x,y)
I got the same message again.
I have tried to convert it to spherical coordinates, it didn't work. I have also tried the arrayfun method as suggested in a forum answer, it didn't work as well.
Can someone please help me?
Thank you!

Your problem is in your function. k contains imaginary numbers, because of this:
sin(3*t).^(0.8)
If you want to make sure it doesn't contain imaginary numbers, you need to increase b. Bottom line is, fix your formula. I can only suppose you mean something like this, but there could be other solutions. Essentially, I think you mean to take the exponent of 1-sin, not sin.
k=a*((1-sin(3*t)).^(2*b))+c;
This gives the following plot (From Octave, but it should be the same)
I figured this out by `plot(k). If k contains imaginary, it will plot the real vs imaginary components. If it is purely real, it will plot the line vs time.

Related

Can't solve ODE in matlab, strange plot occur

enter image description here
I've checked my code, which I think is ok, but can't get the right plot as in the book
here's my code:
clc,clear,close all
syms y(x) dy d2y
dy=diff(y,1)
d2y=diff(y,2)
y=dsolve((1-x)*diff(y,2)==sqrt(1+diff(y,1)^2)/5,y(0)==0,dy(0)==0)
fplot(y,[0,2],'r')
the output of y = dsolve is a vector with 2 equations as its elements. Also in the range of fplot these functions are complex. Since I don't know which results you are looking for, you can try and check these alternatives for your plot and have a look at the output:
fplot(real(y(1)),[0,2],'r')
fplot(abs(y(1)),[0,2],'r')
fplot(imag(y(1)),[0,2],'r')

Plotting modes of circular waveguide in Matlab

I'm having some trouble with a script that I need to plot the TE and TM modes of a circular waveguide.
Everything is based on these formulas:
Right now I'm focused on the TE modes and what I have to plot is the field vector e_mn'' inside a squared-mesh equal to the radius of the waveguide.
To do it I need to compute the bessel function of the first kind and extract x_mn that correspond to the n-th root in which the function is equal to zero. These points are supposed to be real but positive.
I followed this example
https://www.mathworks.com/examples/matlab/community/22719-roots-of-a-bessel-function
but I get an error:
Undefined function 'isfinite' for input arguments of type> 'function_handle'. Error in roots (line 26) if ~all(isfinite(c)) Error
in circular (line 20)
x_mn=roots(J0)
This is my code. Can you help me?
clc
clear all
close all
a=20; %radius
m=0;
n=1;
%%
if m==0
ki_m=1;
else
ki_m=sqrt(2);
end
r=0:0.1:a;
J0 = #(r) besselj(0,r);
%J0 = besselj(m,r);
%plot(J0)
x_mn=roots(J0)
%plot(J0(x_mn))
%x_mn=abs(x_mn);
k_mn=x_mn./a;
F_mn=(ki_m*k_mn)./(J0(x_mn)*sqrt(pi*(x_mn^2-m^2)));
for r=0:1:a
for phi=0:1*pi/180:2*pi
e_mnR=-F_mn.*(J0(k_mn*r)/(k_mn.*r))*sin(m*phi);
e_mnPHI=F_mn.*J0(k_mn*r)*cos(m*phi);
end
end
e_mnR=abs(e_mnR);
e_mnPHI=abs(e_mnPHI);
X=0:1:a;
[X,Y] = meshgrid(1:1:a);
quiver(X,Y,e_mnR,e_mnPHI);
hold on
This is not my area of expertise so take this with a grain of salt, but I did some googling and think I know what's going on:
The code you are working from in the provided link defines:
J0 = chebfun(#(x) besselj(0,x),[0 100]);
Note the link from your reference to a Chebfun example is dead, but I read here that:
Chebfun is an open-source package for computing with functions to about 15-digit accuracy. Most Chebfun commands are overloads of familiar MATLAB commands...
The only reason the example you cited works is because J0 is defined using Chebfun, which overloads the matlab command roots with its own version that works on a function handle. Maybe read more here and try using this tool if you want to replicate that code example. Hope this helps...
You can use fzero instead of roots, but you confused polar and cartesian coordinates in the plot section and dimensions of vectors don't agree

plot a graph in matlab (Matrix dimensionions must agree)

I am new to matlab and I am having the difficulty: I would like to have a graph of a function plotted and 'r' signifies the parabolic equation and valueof 'y' varies and while adding to 'k' it is showing error.
the code is shown below`clear all;
x=[3,4,5,6,7,8,9,10,11,10,13,14,15,16,17,18,19,20,21,22,23];
a=(8.854.*(10.^-12).*(0.016));
y=-0.0925:0.01:0.0925
z=(0.03);
r=((7.3.*(y).^2)+(z));
k=((x.*10^-2))+((r))
c=(a./k);
plot(x,c);
and the error in command window is
Error using +
matrix dimensions must agree.
error in program(line 8)
k=((x.*10^-2))+((r))
how can I get around this problem ?
As people have pointed out in the comments, the matrix dimensions are the issue. If you make x the same dimensions as y (i.e., 1x19), as seen below, you will produce a graph, which is what it sounds like you want:
x=3:21;
a=(8.854.*(10.^-12).*(0.016));
y=-0.0925:0.01:0.0925
z=(0.03);
r=((7.3.*(y).^2)+(z));
k=((x.*10^-2))+((r))
c=(a./k);
plot(x,c);
From your comment: If you want it in 2-D, i.e., one value of k for every pair of x and r then in Matlab 2016a+, all you need is k=((x.*10^-2))+((r).'), i.e., one more transpose. In Pre-2016, you would use arrayfun for this:
[xr,rx] = meshgrid(x,r);
k = arrayfun(#(x,r) ((x.*10^-2))+((r)),xr,rx);
Btw, to plot a 2-D image you would not use plot but imagesc. This is what it looks like:
First: your variable k, second: your variable c. Is this what you were looking for?
As pointed out by Matlab and in the comments, the problem is indeed the difference in size of the vector.
you can either change x or a better solution is to use linspace , as :
y=linspace(-0.925,0.0915,21);

3D Laplace Relaxation in MATLAB

I was able to write a MATLAB program that plots a 1D Laplace relaxation between two metal plates to find equilibrium potential using Jacobi method.
i will be honest, i am not completely sure that i understand what i did, but here is the relevant part of the code:
N=100;
V = zeros(N,N);
V(1,:)=-1;
V(N,:)=1;
for n=1:400
for i=2:99
for j=2:99
V(i,j)=(V(i-1,j)+V(i+1,j)+V(i,j+1)+V(i,j-1))*0.25;
end
end
end
and this is how it looks like:
I am wondering if it is possible to do something similar using the same method, but in 3D. I want to visualize something similar in 3D, a 3D potential... box.
I tried using a similar equation that i found in "Computational Physics. N. J. Giordano & H. Nakanishi, Eq.(5.8)" :
V(i,j,k) = [V(i+1,j,k) + V(i-1,j,k) + V(i,j+1,k) + V(i,j-1,k) + V(i,j,k+1) + V(i,j,k-1)] * (1/6);
and here is the new code that i have been trying to get to work:
N=10; % Used smaller number to reduce processing time.
V = zeros(N,N,N);
V(:,:,1)=-1; %i am using planes instead of axis as "Insulators"
V(:,:,N)=1;
for n=1:100
for i=2:99
for j=2:99
for k=2:99
V(i,j,k)=(V(i+1,j,k)+V(i-1,j,k)+V(i,j+1,k)+V(i,j-1,k)+V(i,j,k+1)+V(i,j,k-1))*(1/6);
end
end
end
end
And i am getting a Index exceeds matrix dimensions. in the line where V(i,j,k) is.
So again, I am trying to get something that is the 3D version of the 2D graph linked above. *Also, i would really appreciate if someone can explain a little bit (not the MATLAB part, but the math part) what is exactly that i am doing here with that equation, and what this could be used for?
Greatly appreciate the help.
Edit: i forgot to ask: how would you graph this 3D array?
You have defined N but are not using it during the process. Since you have values such as i-1 and j-1 you need to start from 2. And for you have values such as i+1 and j+1 you need to end by N-1. So a working code will look like this:
N=10; % Used smaller number to reduce processing time.
V = zeros(N,N,N);
V(:,:,1)=-1; %i am using planes instead of axis as "Insulators"
V(:,:,N)=1;
for n=1:1
for i=2:N-1
for j=2:N-1
for k=2:N-1
V(i,j,k)=(V(i+1,j,k)+V(i-1,j,k)+V(i,j+1,k)+V(i,j-1,k)+V(i,j,k+1)+V(i,j,k-1))*(1/6);
end
end
end
end
Also, the first for-loop seems to be doing nothing in this piece of code that you have provided.

Q on plotting function against t and trajectory in phase space(matlab)

I am very beginner for matlab and try to solve this question. But so far it is not successful. I have spent quite a time and I think I need some help. I would appreciate any help!!!
I need to plot v against time and trajectory of v and w in phase space. The whole question is below and my code for previous question connected to this question is also below. I can go with subplot(2,1,1) for the first graph and subplot(2,1,2) for the next graph. But I am not sure what I have to do other than this. I kind of found ode45 command. But not sure how to use it and if it is the right one to use here. I have tried to use ode45. But it shows many errors that I don't understand.....Please help me on this. Thanks a lot!
'Create a figure which contains two graphs, using subplot. In the first graph, plot the temporal evolution of the membrane potential v(t) against time t. In the second graph, plot the corresponding trajectory (v(t); w (t)) in (the so-called) phase space.'
% my code is below.
a=0.08;
b=3.6;
c=0.7;
T=2; % this can be any number
I_ext=20; % this can be any number
dt=0.01; % this can be any number
function [ v,w ] = fhnn( a,b,c,I_ext,T,dt)
v=zeros(1,numel(T/dt));
w=zeros(1,numel(T/dt));
for n=1:numel(T/dt)
v(n+1)=(v(n)-v(n)^3/3-w(n)+I_ext)*dt+v(n);
w(n+1)=a*(v(n)-b*w(n)+c)*dt+w(n);
end
I gather you have a differential equation and are trying to directly plot that. You might find a better approach would be to actually solve the equation if that is possible.
Either way, recognising that:
numel returns the length of an array and dT/dt is always a scalar so the length is always one.
fhnn is not used here.
You still need a vector t.
If what is in your for loop is correct, the following should work:
a=0.08; b=3.6; c=0.7; T=2; I_ext=20; dt=0.01;
t = 0:dt:T;
v = zeros(1,round(T/dt));
w = zeros(1,round(T/dt));
for n=1:round(T/dt)-1
v(n+1)=(v(n)-v(n)^3/3-w(n)+I_ext)*dt+v(n);
w(n+1)=a*(v(n)-b*w(n)+c)*dt+w(n);
end
subplot(2,1,1)
plot(t,v)
xlabel('t')
ylabel('v')
subplot(2,1,2)
plot(v,w)
xlabel('v')
ylabel('w')