Beginner in matlaB i need lil help

In summary, the conversation is about a beginner seeking help with an equation in MATLAB and requesting tutorials. The equation in question is y=e^x*cosx, and the goal is to plot it for values of x between 0 and pi, as well as for values between -pi and 1, and then combine the two graphs. The person has attempted to do this but encountered errors due to using arrays instead of a loop. They are advised to use a loop and referred to the MATLAB help section for more information on operators and functions.
  • #1
Casablanca
1
0
hello everyBody..
am a beginner in MATLAB i need lil help in the equation blew.. and if anyone has any tutorial for MATLAB i'll b more than welcome to accept them :P

y=e^x . cos x

plot : pi greater than x greater than 0
then plot : -pi smaller than x smaller than one

then combine the two graphs

i tried to do it but i get a lot of errors,,

my trial :
x=0:pi;
y=exp(x)* cos(x)
subplot (3,1,1)
plot (x,y)

x1=-pi:0;
y1=exp(x)* cos(x)
subplot (3,1,2)
plot (x1,y1)

subplot(3,1,3)
plot (y,y1)


=S

thanks in advance..
 
Physics news on Phys.org
  • #2
Your problem is that: in the first line, you declare x as an array. In the second line, you force MATLAB to calculate the exp and cos value for each member of x array. Therefore, MATLAB throws error because exp and cos funtion in MATLAB don't know how to do that but they only accept 1 value for argument per calculations.. To avoid this problem, you should use loop. For example:

Code:
x=0:0.01:pi;

for j = 1 : length(x)              // for loop
    y=exp(x(j))* cos(x(j));     // pick member x(j) to calculate
end;

plot (x,y)

//x(A) will give you value of Ath member of array x (A is integer and smaller than size of x).

----------
Loop is crucial concept for programming that you must obtain, such as FOR loop, WHILE loop, DO loop.

For more information about operators and functions in matlab, check FUNCTION REFERENCE in MATLAB help section.

Hope this help you.
 
  • #3


Hello and welcome to MATLAB! It's great that you're starting to learn this powerful tool. First of all, I recommend checking out the official MATLAB website for tutorials and resources. They have a lot of helpful information for beginners.

Now, let's take a look at your code. It seems like you're on the right track, but there are a few things that need to be corrected. First of all, you should use the ".*" operator when multiplying arrays, instead of just "*". So your code for y and y1 should be:

y = exp(x).*cos(x);
y1 = exp(x1).*cos(x1);

Also, your second subplot should have x1 as the x-axis, not x. So it should be:

subplot(3,1,2)
plot(x1,y1)

Finally, to combine the two graphs, you can simply use the "hold on" command before plotting the second graph. So your final code should look like this:

x = 0:pi;
x1 = -pi:0;
y = exp(x).*cos(x);
y1 = exp(x1).*cos(x1);

subplot(3,1,1)
plot(x,y)
hold on
subplot(3,1,2)
plot(x1,y1)
subplot(3,1,3)
plot(x,y,x1,y1)

I hope this helps and good luck with your MATLAB journey!
 

1. What is Matlab and how do I get started?

Matlab is a programming language and software environment commonly used in scientific and engineering applications. To get started, you can download the software from the MathWorks website and refer to their documentation and tutorials for guidance.

2. How do I create and run a basic Matlab program?

To create a program, you can open the Matlab editor and type in your code. To run the program, you can either click the green "Run" button or use the "run" command in the command window. Make sure to save your program with the .m extension.

3. How do I perform basic calculations in Matlab?

To perform calculations, you can use the built-in functions and operators in Matlab. For example, you can use the plus sign (+) for addition, asterisk (*) for multiplication, and so on. You can also use the command window as a calculator by typing in your calculations directly.

4. How do I plot data in Matlab?

To plot data in Matlab, you can use the "plot" function. This function takes in the x and y values of your data and creates a graph. You can also customize your plot by adding titles, labels, and formatting options.

5. Where can I find help and resources for learning Matlab?

There are many resources available for learning Matlab, including the official MathWorks website, online courses, video tutorials, and forums. You can also refer to the built-in documentation and help feature in the Matlab software for specific questions and troubleshooting.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
993
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top