Beginner in matlaB i need lil help

  • Context: MATLAB 
  • Thread starter Thread starter Casablanca
  • Start date Start date
  • Tags Tags
    Beginner Matlab
Click For Summary
SUMMARY

The discussion focuses on resolving a beginner's issue in MATLAB related to plotting the function y = e^x * cos(x) over specified intervals. The user encountered errors due to incorrect handling of array operations in MATLAB. The solution involves using a for loop to iterate through the array elements, allowing the calculation of y for each corresponding x value. Additionally, the importance of understanding loops in programming is emphasized as a fundamental concept for effective coding in MATLAB.

PREREQUISITES
  • Basic understanding of MATLAB syntax and functions
  • Familiarity with array operations in MATLAB
  • Knowledge of plotting functions in MATLAB
  • Understanding of programming loops, specifically FOR loops
NEXT STEPS
  • Learn about MATLAB array operations and element-wise calculations
  • Study the use of loops in MATLAB, focusing on FOR and WHILE loops
  • Explore MATLAB plotting functions and customization options
  • Review the MATLAB Function Reference for operators and built-in functions
USEFUL FOR

Beginner MATLAB users, students learning programming concepts, and anyone interested in mathematical plotting and function manipulation in MATLAB.

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.
 

Similar threads

Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K