vectors not same size

15 views (last 30 days)
Mike
Mike on 17 Mar 2014
Commented: Walter Roberson on 17 Mar 2014
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 Comment
Image Analyst
Image Analyst on 17 Mar 2014
You forgot to include the actual error message. Paste ALL the red text, ALL of it, back here.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 17 Mar 2014
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 Comments
Mike
Mike on 17 Mar 2014
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 on 17 Mar 2014
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?

Sign in to comment.


Image Analyst
Image Analyst on 17 Mar 2014
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 Comment
Walter Roberson
Walter Roberson on 17 Mar 2014
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!