Beginner in matlaB i need lil help

  • Context: MATLAB 
  • Thread starter Thread starter Casablanca
  • Start date Start date
  • Tags Tags
    Beginner Matlab
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 2K views
Casablanca
Messages
1
Reaction score
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
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.