How to create a nested for loop to create a mesh plot

6 次查看(过去 30 天)
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:
clear all;
clc;
for i=1:501;
x(i)=(i-1)*(20/500)-10;
end;
y1=sin(2.*x)+sin(x);
if y1(y1<-1);
y1(y1<-1)=0;
end;
if y1(y1>1);
y1(y1>1)=0;
end;
plot(x,y1);
a=1;
for m=1:100;
for t=1:m;
f(t)= exp(-a*t);
end;
z(y1)=y1.*f;
end;
This comes back with:
"Subscript indices must either be real positive integers or logicals.
Error in SinLoopIfThen (line 20)
z(y1)=y1.*f;
Am I even understanding the question and if so how can I fix it?

回答(2 个)

Desiree Phillips
Desiree Phillips 2013-4-17
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,
if y1(y1<-1);
y1(y1<-1)=0;
end;
if y1(y1>1);
y1(y1>1)=0;
end;
can be reduced to
y1(y1<-1)=0;
y1(y1>1)=0;
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
Ahmed A. Selman 2013-4-17
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:
clear all
clc
a=1;
z=zeros(501,100);
f=zeros(1,100);
x=zeros(1,501);
for i=1:501
x(i)=(i-1)*(20/500)-10;
y1=sin(2.*x)+sin(x);
if y1(y1<-1)
y1(y1<-1)=0;
end
if y1(y1>1)
y1(y1>1)=0;
end
for t=1:100
f(t)= exp(-a*t);
end
z(i,:)=y1(i)*f;
f=zeros(1,100); % resetting f
end
plot(x,y1);
figure
mesh(z)
xlabel('time');ylabel('Signal');zlabel('Amplitude');
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
a=7.4200
then
min(min(z(:,100))) = -4.0019e-322
at a=7.5000, Matlab finds that the minimum is exactly 0.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by