Plotting y=x^n in MATLAB with Loops

  • Thread starter Thread starter fball558
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
SUMMARY

The discussion focuses on creating subplots of the function y=x^n in MATLAB for n ranging from 1 to 6 and x values from -10 to 10. The initial attempt used nested for loops but resulted in a blank graph due to incorrect element-wise operations. The correct approach involves using the element-wise power operator (.^) to compute y values for each n, and plotting them in a single command. Additionally, MATLAB's help function is recommended for understanding specific functions.

PREREQUISITES
  • Basic understanding of MATLAB syntax and commands
  • Familiarity with vector and matrix operations in MATLAB
  • Knowledge of plotting functions in MATLAB
  • Understanding of element-wise operations in programming
NEXT STEPS
  • Learn MATLAB element-wise operations using the dot operator (e.g., .^, ./)
  • Explore MATLAB's subplot function for creating multiple plots
  • Investigate MATLAB's cell arrays for storing multiple vectors
  • Review MATLAB documentation on plotting and arithmetic operators
USEFUL FOR

Students and educators in mathematics or engineering fields, MATLAB programmers, and anyone interested in visualizing mathematical functions using MATLAB.

fball558
Messages
143
Reaction score
0
anyone know MATLAB?

Homework Statement


Using Matlab, write an m-file that creates subplots of y=x^n for n=[1:6] and
x = [-10:0.01:10]. Each subplot needs appropriate labels in bold and using superscripts. this should also be commanded from within a loop and using num2str commands where necessary.



The Attempt at a Solution



i started out with a nested for loop as followed.

for x = [-10:0.01:10]
for n=[1:6]
y=x^n
plot(y) trying to just graph what I am getting so far in the loop
end
end


when i run the program i get just a blank graph??
any help would be great!
 
Physics news on Phys.org
fball558 said:

Homework Statement


Using Matlab, write an m-file that creates subplots of y=x^n for n=[1:6] and
x = [-10:0.01:10]. Each subplot needs appropriate labels in bold and using superscripts. this should also be commanded from within a loop and using num2str commands where necessary.



The Attempt at a Solution



i started out with a nested for loop as followed.
Code:
for x = [-10:0.01:10]
   for n=[1:6]
      y=x^n
      plot(y)          [b]%[/b]trying to just graph what I am getting so far in the loop
    end
end

when i run the program i get just a blank graph??
any help would be great!

Well, the beauty of MATLAB is that you can run operations on vectors and arrays, and don't have to do element-wise operations (most of the time), as you might in C or C++ (or whatever your intro computing course was).

So, you could do something like the following:
Code:
x=[0:0.01:10]
y=x.^1     %note the period before the ^, this tells MATLAB to raise each element of x to the power of 1

plot(y,x)
pause(1)     %pauses a second

y=x.^2
plot(y,x)

Now, you could pack each of your y-vectors into a 2-D matrix (array), and sort things out later, or you can use a cell array (the curly braces), and store the vectors separately.

Code:
x=[0:0.1:10];
for n=1:6
     y{n}=x+1;
end
plot(x,y{1}, x,y{2}, x,y{3}, x,y{4}, x,y{5}, x,y{6})

BTW, a great way to find what MATLAB functions do is just to type 'help' and the name of the function, for instance:
>> help plot

...which brings up the help file for the Plot function. More in-depth documentation is available at the MATLAB website (I found this invaluable when I was a MATLAB programmer one summer--bookmark the top page!):
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/arithmeticoperators.html
 

Similar threads

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