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

In summary: I initialize i = 0 and increment it for each new value of c. Then I use F(i,c) to get the value of exp(-c*x). I can plot this data just fine.In summary, the author attempted to solve a problem involving plotting the exponential function, but ran into some difficulties. For looping purposes, they used an array instead of a for loop, but needed to initialize i before using it as an index. Additionally, they ran into trouble with indexing x within the for loop. Finally, they resolved the issue by creating a script that works fine.
  • #1
BubblesAreUs
43
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
  • #2
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.
 
  • #3
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...
 
  • #4
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);
 
  • #5
Thanks Choppy.

I got the script working.
 

What is the purpose of plotting exp(-cx) with MATLAB?

The purpose of plotting exp(-cx) with MATLAB is to visualize the exponential function with a negative input value, c. This function is commonly used in mathematical and scientific contexts to model decay or growth processes.

What are the steps for plotting exp(-cx) with MATLAB?

The steps for plotting exp(-cx) with MATLAB are as follows:

  1. Define the value of c.
  2. Create a vector of x values using the "linspace" function.
  3. Calculate the exp(-cx) values using the "exp" function.
  4. Plot the x and exp(-cx) values using the "plot" function.
  5. Add labels and title to the plot using the "xlabel", "ylabel", and "title" functions.
  6. Display the plot using the "show" function.

Can the value of c be changed in the plot?

Yes, the value of c can be changed in the plot by simply redefining it in the first step of the plotting process. This allows for easy comparison of the exponential function with different input values of c.

Is it possible to add multiple plots of exp(-cx) with different values of c on the same graph?

Yes, it is possible to add multiple plots of exp(-cx) with different values of c on the same graph. This can be done by repeating the plotting process for each value of c and using the "hold on" function to add each plot to the same graph.

Can this step-by-step guide be used for other functions in MATLAB?

Yes, this step-by-step guide can be used as a general guide for plotting any function in MATLAB. The specific function name and input values will need to be changed, but the overall steps for creating and displaying the plot will remain the same.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
826
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
954
  • Engineering and Comp Sci Homework Help
Replies
1
Views
881
  • Engineering and Comp Sci Homework Help
Replies
3
Views
806
  • Engineering and Comp Sci Homework Help
Replies
6
Views
855
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
Back
Top