How to run a for loop n times with a different size matrix inside the loop

1 回表示 (過去 30 日間)
BryanE
BryanE 2018 年 9 月 5 日
編集済み: Koundinya 2018 年 9 月 11 日
I am attempting to run the four values of x_o and y_o through the loop to do conformal mapping. The matrix size of the circle in the loop is different, but I need to run it four times, with four different output graphs. Is this possible in MATLAB? Thanks
Here is what I have so far:
x_o = [0.1, 0.15, 0.2, 0.25]; y_o = [0.1, 0.15, 0.2, 0.25]; n = length(x_o);
for i = 1:n
R = 1;
theta = linspace(0,2*pi,100);
x = R*cos(theta) - x_o;
y = R*sin(theta) + y_o;
b = sqrt(R^2 - y_o.^2) + x_o;
A = x + [(x.*b.^2)./(x.^2 + y.^2)];
B = y - [(y.*b.^2)./(x.^2 + y.^2)];
figure
plot(x,y,'--',A,B)
grid on
end
  3 件のコメント
BryanE
BryanE 2018 年 9 月 5 日
My problem is that the matrix don't agree, is there a way to setup the loop to avoid this?
Stephen23
Stephen23 2018 年 9 月 5 日
"My problem is that the matrix don't agree"
Don't agree with what?
Please given an actual example of the inputs and outputs that you expect to get.

サインインしてコメントする。

採用された回答

Koundinya
Koundinya 2018 年 9 月 10 日
編集済み: Koundinya 2018 年 9 月 11 日
You must be getting the error ' Matrix dimensions must agree ', this is because
theta = linspace(0,2*pi,100);
creates a vector theta with dimensions 1x100, R*cos(theta) and R*sin(theta) will also have dimensions equal to 1x100 , but your vector x_o is of dimension 1x4, so adding/subtracting vectors of unequal dimensions will result in an error
% Error occurs at these lines
x = R*cos(theta) - x_o;
y = R*sin(theta) + y_o;
b = sqrt(R^2 - y_o.^2) + x_o;
to iterate through each element of the vectors x_o and y_o, change it to
x = R*cos(theta) - x_o(i);
y = R*sin(theta) + y_o(i);
b = sqrt(R^2 - y_o(i).^2) + x_o(i);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by