フィルターのクリア

vectors not same size

4 ビュー (過去 30 日間)
Mike
Mike 2014 年 3 月 17 日
コメント済み: Walter Roberson 2014 年 3 月 17 日
Hello,
I'm writing a code for a monte carlo simulation for trying to find a solution to a PDE problem.
I have the code mostly done and am trying to run it but i keep getting an error with my plot. Says my vectors are not the same size. When i go and try to change on vector to match the other, the one that i am trying to match goes up by 1 value. Not sure how to fix this, but any help would be appreciated.
clear;
clc;
n = 50;
x = linspace(0,1,n);
xo = zeros(1,n);
Xinside = [];
dist_X = [];
figure(1)
clf;
syms p
bc = sin (p);
Range = [0 pi];
h = ezplot(bc,Range);
hold on
LeftRightUpDown = (rand(1)./4);
y1 = LeftRightUpDown;
%x = x0;
X = [];
X = [X;xo];
%y2 = path(xo,n);
for i=1:n
x = x + LeftRightUpDown();
X = [X;x];
end
X;
zero = 0;
one = 1;
%while x + LeftRightUpDown() - 0.1;
if x<=0
y = zero;
elseif x>=0
y = one;
end
%end
s = 0;
for i=1:n
s = s + x;
y5 = s/(1.0*n);
end
%plotting the path of solution
numberofsteps = n;
numtrial = 1;
x = linspace(0,1,n);
ysol = [s(numtrial,numberofsteps), x];
plot(x,ysol);
thanks in advanced
  1 件のコメント
Image Analyst
Image Analyst 2014 年 3 月 17 日
You forgot to include the actual error message. Paste ALL the red text, ALL of it, back here.

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

回答 (2 件)

Walter Roberson
Walter Roberson 2014 年 3 月 17 日
You have
ysol = [s(numtrial,numberofsteps), x]
if s is empty then you would get an indexing error. As you did not, s() must have at least one element in it. So ysol will be At least one element plus all of the elements of x
plot(x,ysol)
x is whatever length it is, and ysol is at least one element longer than x. The vectors are thus always going to be different lengths. There is no solution other than to define ysol a different way that is the same length as x.
  2 件のコメント
Mike
Mike 2014 年 3 月 17 日
So would it make sense to define s as a cell or a vector or zeros the same size as x?
Walter Roberson
Walter Roberson 2014 年 3 月 17 日
At the moment I do not understand why you want y to have the content that x used to have, together with one more element.
On the other hand I don't understand why you have
for i=1:n
s = s + x;
y5 = s/(1.0*n);
end
as you are overwriting all of y5 in each step of the loop, and you never use y5 afterwards. Why not just use
s = sum(x);
instead of looping to sum?

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


Image Analyst
Image Analyst 2014 年 3 月 17 日
Try this:
ysol = [s(numtrial,numberofsteps), x];
% Now make x exactly as long as ysol is.
x = linspace(0, 1, length(ysol));
% Now they're the same size and we can plot ysol vs. x
plot(x, ysol, 'bo-', 'LineWidth', 2);
grid on;
  1 件のコメント
Walter Roberson
Walter Roberson 2014 年 3 月 17 日
If done in a loop this will have the effect of making x one longer at each step, and "s" would always get associated with 0.

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

カテゴリ

Help Center および File ExchangeGeometry and Mesh についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by