Drawing Complex Function Graph in MATLAB

In summary, the individual was having trouble plotting a complex function in MATLAB. They had some errors in their code, but were able to eventually successfully plot the function using a combination of linspace and plot functions. They also had difficulty understanding the differences between MATLAB and Mathematica, but were able to use online resources to better understand MATLAB's syntax.
  • #1
adnan jahan
96
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
  • #2
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?
 
  • #3
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)
 
  • #4
so, what is the value of x at the time you evaluate U?
 
  • #5
"x" is the having range from 0-5
 
  • #6
no...you do that AFTER you evaluated U
 
  • #7
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.
 
  • #8
"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
 
  • #9
"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.
 

Related to Drawing Complex Function Graph in MATLAB

1. How do I plot a complex function graph in MATLAB?

To plot a complex function graph in MATLAB, you can use the plot or ezplot function. First, define your complex function as a symbolic expression using the syms function. Then, use the plot function to plot the real and imaginary parts of the function, or use the ezplot function to plot the magnitude and phase of the function.

2. Can I customize the appearance of the complex function graph?

Yes, you can customize the appearance of the complex function graph in MATLAB using various properties and options. For example, you can change the line color, style, and thickness using the Color, LineStyle, and LineWidth properties. You can also add a title, labels, and legend using the title, xlabel, ylabel, and legend functions.

3. How do I add multiple complex functions to the same graph?

You can add multiple complex functions to the same graph by using the hold function. First, plot the first function using the plot or ezplot function. Then, use the hold on command to hold the current plot and add more functions using the same plot or ezplot function. Finally, use the hold off command to turn off the hold function and display all the functions on the same graph.

4. Can I save the complex function graph as an image?

Yes, you can save the complex function graph as an image in MATLAB by using the saveas function. This function allows you to save the current figure as an image file in various formats, such as PNG, JPEG, or PDF. You can also use the print function to save the graph as an image with customized settings, such as resolution and size.

5. How can I add a grid to the complex function graph?

To add a grid to the complex function graph in MATLAB, you can use the grid function. Simply call this function after plotting the function, and it will add a grid to the current graph. You can also customize the grid appearance by changing the color, style, and spacing using the GridColor, GridLineStyle, and GridSpacing properties.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
137
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
2K
  • Calculus and Beyond Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
7K
Replies
1
Views
2K
Back
Top