MATLAB: How to correct matrix dimension disagree error

matrix dimension must agree

figure
clear;
clc;
v = 1.0;
n=[0.00 0.10 0.12 0.18];
u = linspace(0,2);
%%

for i = 1:length(n)
sigma = ((1i.*u-1-v.^2)./(v.^2-u.^2 +1-2i.*u)).*(1./(v.^2 +1));
sigmapri = n*((1i.*u-1-v.^2)./((1+n).^2 +v.^2).*(v.^2-u.^2 +1-2i.*u)).*(1-(v./(v.^2 +1)));
w = sigma + sigmapri;
plot(u, real(w))
end
%%
xlabel('\omega*\tau')
ylabel('\sigma(\omega)/\sigma_o')

Best Answer

I think you're looking for this:
hFig = figure
clc;
v = 1.0;
n=[0.00 0.10 0.12 0.18];
u = linspace(0,2, 30); % However many you want.
legendStrings = cell(length(n), 1);
for k1 = 1:length(n)
thisN = n(k1);
for k2 = 1 : length(u)
thisU = u(k2);
sigma = ((1i.*thisU-1-v.^2)./(v.^2-thisU.^2 +1-2i.*thisU)).*(1./(v.^2 +1));
sigmapri = thisN * ((1i.*thisU-1-v.^2)./((1+thisN).^2 +v.^2).*(v.^2-thisU.^2 +1-2i.*thisU)).*(1-(v./(v.^2 +1)));
w(k2) = sigma + sigmapri;
end
legendStrings{k1} = sprintf('n = %.2f', thisN);
plot(u, real(w), '.-', 'LineWidth', 2, 'MarkerSize', 15);
hold on;
drawnow;
end
grid on;
fontSize = 20;
xlabel('\omega*\tau', 'FontSize', fontSize)
ylabel('\sigma(\omega)/\sigma_o', 'FontSize', fontSize)
title('\sigma(\omega)/\sigma_o vs. \omega*\tau', 'FontSize', fontSize)
legend(legendStrings, 'Location', 'northwest');
% Maximize the figure window.
hFig.WindowState = 'maximized';