MATLAB: Please help with plotting and for loops!

for loopplot

Hi,
I'm new to Matlab so, I'm trying my best! Thank you in advance for the help.
This is my code:
frequency0 = 10e9;
lambda0 = 3e8 / frequency0;
l = 0.5 * lambda0;
l = l * 39.3701;
n = 10;
delta = l/n;
Z1 = 50;
Z2 = 100;
for x = [0:delta:l]
Z0 = Z1 + (Z2 – Z1)*(x/l);
Zin_linear = Z0 * (((Z2 * cos(B* x)) + (j * Z0 * sin(B * x))) / ((Z0 * cos(B*x)) + (j * Z2 * sin(B*x))));
Z2 = Zin_linear;
endfor
plot(x,Z0);
What I'm trying to do, is for each number in x, I want to calculate the Z0 and the Zin_linear. Then I want to take Z2 and replace it with Zin_linear and then calculate a new Z0 and I want to do this until we reach l. However, when I plot this, I get one point on the graph. What am I doing wrong?

Best Answer

hello
this is the typical situation when you forget to index the variable used to do the plot after the for loop
just missing the real B value (not provided) , but the code seems to work now
all the best,
frequency0 = 10e9;
lambda0 = 3e8 / frequency0;
l = 0.5 * lambda0;
l = l * 39.3701;
n = 10;
delta = l/n;
Z1 = 50;
Z2 = 100;
x_list = (0:delta:l);
B = 1; % put the right value here
Z0_plot = zeros(size(x_list)); % preallocation (always better)
for ck = 1:numel(x_list)
x = x_list(ck);
Z0 = Z1 + (Z2 - Z1)*(x/l);
Zin_linear = Z0 * (((Z2 * cos(B* x)) + (1i * Z0 * sin(B * x))) / ((Z0 * cos(B*x)) + (1i * Z2 * sin(B*x))));
Z2 = Zin_linear;
% for storage and plot
Z0_plot(ck) = Z0;
end
plot(x_list,Z0_plot);