Plotting exp(-cx) with MATLAB: Step-by-Step Guide for Homework

  • Thread starter Thread starter BubblesAreUs
  • Start date Start date
  • Tags Tags
    Matlab Plot
Click For Summary

Discussion Overview

The discussion revolves around a homework problem involving the plotting of the exponential function exp(-cx) for values of c ranging from 1 to 4, over the interval 0 ≤ x ≤ 1. Participants explore MATLAB coding techniques, particularly the use of loops and array indexing.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant expresses uncertainty about how to start the coding task and shares an initial attempt at a solution using nested loops.
  • Another participant points out that the variable F will only hold a single value due to the way it is calculated in the loop and suggests using an array for F to store multiple values.
  • There is a suggestion to modify the x variable to use a finer increment (0.01) to get more points in the plot.
  • A participant questions the need for initializing a counter variable i and how to properly nest it within the loops.
  • Clarifications are provided regarding the nature of the x variable, indicating that it creates an array rather than functioning as a loop, and the necessity of indexing when using it in calculations.
  • Alternative approaches are discussed, including the use of array operations in MATLAB to avoid the need for explicit loops.

Areas of Agreement / Disagreement

Participants generally agree on the need to adjust the coding approach to correctly plot the function, but there are differing opinions on the necessity of using loops versus array operations. The discussion remains unresolved regarding the best coding practices for this specific problem.

Contextual Notes

Limitations include the potential misunderstanding of how MATLAB handles arrays and loops, as well as the need for proper indexing when accessing elements within arrays.

BubblesAreUs
Messages
43
Reaction score
1

Homework Statement



Plotting the exponential exp(-cx), 0 ≤ x ≤ 1 with c = 1, 2, 3, 4. Use a for loop.

Homework Equations

The Attempt at a Solution



for c = 1:1:4;
for x = 0:1;
F = exp(-cx);
end
end

plot(x,F)

I'm not really sure where to start..

Help is appreciated.
 
Physics news on Phys.org
The way you have it written, F is going to be recalculated for each loop and hold only that single value until the next time it is calculated.
Also while the c variable will increment through integer values as desired, x will also only take the values 0 and 1.

To solve the second problem, try:
for x = 0:0.01:1;

To solve the first you might want to try setting F up as an array, such that F(i,c) = exp(-cx); You would then have to establish independent counter "i" that increments i = i +1 at the beginning of the x loop, and i = 0 at the beginning of the c loop.

With MATLAB you generally don't need to use for loops to solve a problem like this, but perhaps the loop are one point in the exercise.
 
So the array x = 0:0.01:1 is actually a loop? I can see that, but how would I nest a counter into that?

Moreover, why would I have to initialise i = 0 in the beginning of the c loop?

Reformed code...

x = 0:0.1:1; <--- Acts as a loop from 0 to 1, 0.1 increments
i = i + 1;

for c = 1:1:4;
i = 0 <---not sure why?
F(i,c) = exp(-c*x);

plot(x,F(c,x))end

Thanks...
 
x = 0:0.1:1; % This line doesn't act as a loop the way you have it now. It makes x an array with values [0.0 0.1... 1.0]. You can keep it a loop if you have to with a "for" statement out front, but if you're going to do that, I would nest it back inside the c loop as you had it before.

You initialize i = 0 for each new value of c. Then increment it for each new value of x. You have to do this before you use it as an index because you can't have an index value of 0.

You'll also have to index x in your F expression if you keep x as a loop, ie. F(i,c) = exp(-c*x(i));

Alternatively if you use x as an array, you don't need the increment counter at all. You would just use the expression:
F(1:length(x),c) = exp(-c.*x);
 
Thanks Choppy.

I got the script working.
 

Similar threads

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