Plotting y=x^n in MATLAB with Loops

  • Thread starter fball558
  • Start date
  • Tags
    Matlab
In summary, the conversation is about using MATLAB to create subplots of y=x^n for n=[1:6] and x = [-10:0.01:10]. The attempt at a solution involves a nested for loop and using the plot function, but the output is a blank graph. The expert suggests using vector and array operations in MATLAB and provides an example using y=x.^1 and y=x.^2. They also mention using cell arrays and suggest looking at the help documentation for more in-depth information.
  • #1
fball558
147
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
  • #2
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
 
  • #3


Hello, thank you for sharing your attempt at solving this problem. I would suggest breaking down the problem into smaller steps and checking for errors along the way.

First, let's focus on creating a single subplot for a specific value of n. We can use the "hold on" command to plot multiple curves on the same figure. So, our code for a single subplot would look something like this:

x = [-10:0.01:10]; % define x values
n = 2; % choose a specific value of n
y = x.^n; % calculate y values
plot(x, y) % plot the curve
xlabel('x') % add x label
ylabel(['y = x^', num2str(n)]) % add y label with superscript using num2str

Next, we can use a for loop to create subplots for different values of n. We can also use the "subplot" command to specify the position of each subplot. So, our updated code would look like this:

x = [-10:0.01:10]; % define x values
figure % create a new figure
for n = 1:6 % loop through values of n
y = x.^n; % calculate y values
subplot(2,3,n) % create subplot at position n
plot(x, y) % plot the curve
xlabel('x') % add x label
ylabel(['y = x^', num2str(n)]) % add y label with superscript using num2str
end

I would also recommend checking for errors along the way, such as making sure the "hold on" command is included and checking the syntax of the "subplot" command. Additionally, you can use the "disp" command to print out the values of x and y at each iteration of the loop to make sure they are correct.

I hope this helps and good luck with your further attempts!
 

1. How do I plot y=x^n in MATLAB using a loop?

To plot y=x^n in MATLAB using a loop, you can use the "for" loop structure. First, define the values of x and n that you want to use for your plot. Then, use the "for" loop to calculate the y values for each x value using the power operator (^). Finally, use the "plot" function to plot the x and y values.

2. Can I use a while loop to plot y=x^n in MATLAB?

Yes, you can also use a while loop to plot y=x^n in MATLAB. The process is similar to using a for loop, except you will need to define a counter variable and increment it within the loop. Additionally, you will need to use the "hold on" and "hold off" commands to ensure all the points are plotted on the same graph.

3. How do I change the range of x values in my plot?

To change the range of x values in your plot, you can use the "xlim" function. This function takes in a vector with two elements, the first being the lower bound and the second being the upper bound. For example, if you want to plot y=x^n for x values between -10 and 10, you would use xlim([-10, 10]).

4. Can I change the color or style of my plot?

Yes, you can change the color and style of your plot by using additional arguments in the "plot" function. For example, you can use "plot(x, y, 'r--')" to plot a red dashed line. You can also use other line styles like 'b-', 'g:', etc. Additionally, you can use the "color" argument to specify a specific color, or the "linewidth" argument to change the thickness of the line.

5. How can I add a title and labels to my plot?

You can add a title and labels to your plot by using the "title", "xlabel", and "ylabel" functions. These functions take in a string as an argument, which will be the title or label that is displayed on your plot. For example, you can use "title('Plot of y=x^n')" to add a title to your plot. Additionally, you can use the "legend" function to add a legend if you have multiple plots on the same graph.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
943
  • Engineering and Comp Sci Homework Help
Replies
7
Views
891
  • Engineering and Comp Sci Homework Help
Replies
3
Views
815
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
829
  • Engineering and Comp Sci Homework Help
Replies
1
Views
890
  • Engineering and Comp Sci Homework Help
Replies
1
Views
961
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
Back
Top