MATLAB Drawing Complex Function Graph in MATLAB

AI Thread Summary
The discussion revolves around plotting a complex function in MATLAB. The user initially struggles with syntax and function declaration, leading to confusion about how to properly define and plot the function U against x. Key issues identified include the incorrect declaration of complex numbers (missing 'i' in some terms), the improper use of the variable x before its definition, and the need to declare U as a function of x for MATLAB to recognize it correctly. After receiving guidance, the user successfully modifies the code to correctly plot U. They also inquire about plotting an additional function W alongside U, but face challenges with the syntax. The conversation highlights the differences between MATLAB and Mathematica, emphasizing that MATLAB is more computation-focused and requires a different approach to function handling. The user expresses gratitude for the assistance received.
adnan jahan
Messages
93
Reaction score
0
Dear fellows
I need to draw a graph of a complex function in matlab, but could not do so, can you please help me in this issue??
I have mentioned each and every constant required for function(complex function),
y1=1.9417+3.5012i;
y2=1.9417+3.5012i;
y3=4.7348-1.4837;
y4=4.7345+1.4837;
y5=202.701;
M1=1.558*10^12-1.90339*10^12i;
M2=1.536*10^12+1.8116*10^12;
M3=-6.0446*10^11+4.0463*10^12i;
M4=-4.134*10^11-3.954*10^12i;
M5=-2.674*10^8-1.222*10^8i;
a=1;
O1=0.023475-0.0252192i;
O2=0.023475+0.0252192i;
O3=-0.0225775-0.0197677i;
O4=-0.0225775+0.0197677i;
O5=-0.000015578;
w=2;
t=0.1;
z=1;
J=(1i*a*O1-y1)*M1*Exp(-y1*x)+(1i*a*O2-y2)*M2*Exp(-y2*x)+(1i*a*O3-y3)*M3*Exp(-y3*x)+(1i*a*O4-y4)*M4*Exp(-y4*x)+(1i*a*O5-y5)*M5*Exp(-y5*x);
U=J*Exp(wt+iaz);
x=linspace(0,5);
plot(x,Re(U))

Any kind of help will be appreciated,

Thanks
 
Physics news on Phys.org
I don't use matlab, but I will point out a couple of things in your post which may or may not be in your actual MATLAB script (did you copy and paste or re-typed?).

y3 and y4 seem to have the i missing in the second term
you do not have proper MATLAB function declared, it seems you are trying to plot a variable, not a function (I know what you mean though, but let's be proper).
you define the variable x AFTER you use it to create J, maybe if you would move the line "x=linspace(0,5); " up a couple of rows, things will work?
 
I copied it from the MATLAB "m-file", the values of y1,2,3,4,5 are constants may be complex may be real,
the function to plot is (real part of "u" along "y-axis" and "x" along "x-axis")

U is a complex function of "x"
as U= a(x)+i b(x)
 
so, what is the value of x at the time you evaluate U?
 
"x" is the having range from 0-5
 
no...you do that AFTER you evaluated U
 
what about my statement that U is not really a MATLAB function? You as a human know that U depends on x, but you have simply evaluated an equation with x in it, calculated a value and assigned that value to a variable U

...at least that the way I see it.

if you want U to be a proper MATLAB funtion of x, I think you need to delcare it as so (need keyboard 'function', I think) and show arguments, etc...and THEN, you can pass x at the time of plotting, something like plot(x, Re(U(x)) ), I presume.
 
"U" is a function of x and I want to draw graph of U against "x",
If i evaluate U for some fixed value of "x" then you can fix any value for "x" that could be 3
 
"U" is a function of x ...
But -- here's the problem (or one of the problems) -- it isn't. Not in MATLAB, that is. You haven't told MATLAB that U is a function of x, not in any way that MATLAB understands.

Have you ever plotted anything in MATLAB? Can you, for instance, plot y = x^2?

gsal is right: the syntax that you posted makes no sense as a way of generating a plot. Work out how to do that first.
 
  • #10
Ohh, ok
I am using MATLAB first time, normally I used mathematica ,
Ok I will try y=x^2 first and try to understand the language of MATLAB,

In the equation which I wrote up there, U is represented in terms of J and J is depending on x
Thanks brothers for your kind support, if still you think of any way to teach me how to draw this function, Please let me now

Regards
 
  • #11
friends, I did it.
y1=1.9417+3.5012*i;
y2=1.9417+3.5012*i;
y3=4.7348-1.4837*i;
y4=4.7345+1.4837*i;
y5=202.701;
m1=1.558*10^12-1.90339*10^12*i;
m2=1.536*10^12+1.8116*10^12*i;
m3=-6.0446*10^11+4.0463*10^12*i;
m4=-4.134*10^11-3.954*10^12*i;
m5=-2.674*10^8-1.222*10^8*i;
a=1;
O1=0.023475-0.0252192*i;
O2=0.023475+0.0252192*i;
O3=-0.0225775-0.0197677*i;
O4=-0.0225775+0.0197677*i;
O5=-0.000015578;
w=2;
t=0.1;
z=1;

x=1:1:5;
j=(i*a*O1-y1)*m1*exp(-y1*x)+(i*a*O2-y2)*m2*exp(-y2*x)+(i*a*O3-y3)*m3*exp(-y3*x)+(i*a*O4-y4)*m4*exp(-y4*x)+(i*a*O5-y5)*m5*exp(-y5*x);
u=j*exp(w*t+i*a*z);
% x=linspace(0,5);
plot(x,u,'bo-')

Thanks for your help
 
  • #12
once again, dear fellows if i have any other function say W and want to plot u and W both on one graph what would be the command for that,
I have used plot ( x, u, 'b*-',W, 'ro-' ) but it did not work,
where W = j*5*O1
 
  • #13
adnan jahan said:
I am using MATLAB first time, normally I used mathematica ,
There's this common perception that MATLAB and Mathematica are alternative systems that do pretty much the same thing. This is wrong. They work very differently, they're programmed very differently, and they're not alternatives. Mathematica is really specialized for doing math. It works with symbols. MATLAB is for doing computation, and is more like a conventional programming language. It can't work with symbols. It may seem like it does, but actually, in MATLAB, as in most programming languages, a name like U is just a way of designating where in the computer something is stored.
 
  • #14
not to be mean, but there is also something in MATLAB called on-line help that for basic stuff like this I bet it works wonders...just search for function.
 
  • #15
I got it and thanks brothers it really help.
 
Back
Top