필터 지우기
필터 지우기

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 CenterFile Exchange에서 Geometry and Mesh에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by