Drawing Complex Function Graph in MATLAB

Click For Summary

Discussion Overview

The discussion revolves around drawing graphs of complex functions in MATLAB, focusing on the correct syntax and methodology for plotting. Participants explore issues related to variable declaration, function definitions, and the differences between MATLAB and other computational software like Mathematica.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant requests assistance in plotting a complex function in MATLAB, providing specific constants and the function definition.
  • Another participant points out potential issues in the MATLAB script, including missing imaginary units and the order of variable definitions.
  • There is a discussion about whether the variable U is properly defined as a function of x in MATLAB, with some participants arguing it is not recognized as such by the software.
  • One participant suggests that U should be declared as a proper MATLAB function to allow for correct plotting.
  • A later reply indicates that the participant has successfully modified their code to plot the function, showing appreciation for the help received.
  • Another participant inquires about how to plot multiple functions on the same graph, indicating a need for further clarification on MATLAB commands.
  • There is a discussion about the differences between MATLAB and Mathematica, emphasizing their distinct functionalities and programming approaches.
  • Some participants suggest utilizing MATLAB's online help for basic syntax and function usage.

Areas of Agreement / Disagreement

Participants express differing views on the proper way to define and plot functions in MATLAB. While some agree on the need for proper function declarations, others emphasize the challenges faced by beginners in understanding MATLAB's syntax. The discussion remains unresolved regarding the best practices for plotting multiple functions simultaneously.

Contextual Notes

There are limitations in the discussion regarding the assumptions made about MATLAB's function handling and the specific syntax required for plotting. Some participants may have varying levels of familiarity with MATLAB, which affects their understanding of the issues raised.

Who May Find This Useful

Individuals new to MATLAB, particularly those transitioning from other computational software, as well as those interested in plotting complex functions and understanding MATLAB's syntax and capabilities.

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.
 

Similar threads

Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K