How to create a nested for loop to create a mesh plot
18 views (last 30 days)
I am trying to finish this problem for a beginner class, so it can't have too difficult of code and I can't seem to figure out what is asked and how to make it happen.
Question: Using a nested for loop,multiply the vector y1 with the decay e-at,for 1<t<100 and a=1,and create the array z. Plot z using the command “mesh”. Find the minimum value of a, for which all the values of z at time 100 are approximately zero.
My Answer So Far:
This comes back with:
"Subscript indices must either be real positive integers or logicals.
Error in SinLoopIfThen (line 20)
Am I even understanding the question and if so how can I fix it?
Desiree Phillips on 17 Apr 2013
The error comes up because your passing y1 to z as if it were an index (which must be a "real positive integer or logical"- a single constant) but it is a vector. Maybe
z(m) = y1.*f;
is what you meant. Going over all of your code, I think Matrix Indexing would be a great link for you to go over. There's a couple of things you can do to reduce the lines of code as well:
Matlab supports indexing using logical statements (e.g.: y1>1 - refer to link above), so the if statements aren't needed. Therefore,
can be reduced to
As for the question you are trying to solve, checking the Mesh Plot page, you can see that mesh(z) has the requirement that z be a matrix.
The question seems to be asking you to make a z matrix where each row (or column) of z is the product of y1 and e^(-at) for each time t.
Is what you posted the exact wording of the question? It seems a bit confusing. Can you post the whole thing (sorry if its lengthy)?
Ahmed A. Selman on 17 Apr 2013
I'm not sure what are your conditions (in the if statements), but you used (y1) as a matrix index which cannot be made unless (y1) varies as 1, 2, 3.. (positive real integer, and larger than zero). But I think the following is what you're looking for:
f=zeros(1,100); % resetting f
title('Damping some signal with exponential decay function','fontWeight','bold');
And, theoretically, the value of (a) that makes (z at t=100) become zero is (infinity), since
exp(-a*t) = zero
if and only if (-at) goes to (- infinity), and since (t) is finite (t=100), so (a) must be as mentioned (I think you meant the largest value of variable a instead of the smallest). But you can assign large value to (a) that makes Matlab unable to recognize it from absolute zero, as
min(min(z(:,100))) = -4.0019e-322
at a=7.5000, Matlab finds that the minimum is exactly 0.