How to create a matrix from a for loop answer?
1 次查看(过去 30 天)
显示 更早的评论
Hello,
I was wondering how i would create a matrix out of solutions from a for loop. Im trying to plot a meshgrid/surface and i need the final results to be a matrix to do this. Any thoughts on how i could make that happen?
Here is the code giving me trouble
%--Solving for radius to see if BC's are hit--%
radius = sqrt(x.^2 + y.^2);
if radius >=1
A = sin(theta);
elseif radius < 1
A = 0;
end
sol = A;
mesh(X,Y,sol)
All of the code if below.
clear;
clc;
%--Number of simulations--%
n = 100;
sol = zeros(1,n);
y = zeros(1,n);
x = zeros(1,n);
%--Plot of the circle--%
figure(1)
clf;
syms p
bc = sin (p);
bc2 = cos (p);
Range = [0 2*pi];
subplot(211);
h = ezplot(bc,bc2,[Range]);
hold on
[X,Y] = meshgrid(-1:0.1:1);
%
Nsamples = 5;
distance = zeros(n,Nsamples);
circle = zeros(n,Nsamples);
delta = 0.1;
for t=1:n
%--Monte Carlo Random Walk--%
monte = randi(4,1);
if monte == 1
y = y + delta;
elseif monte == 2
y = y - delta;
elseif monte == 3
x = x + delta;
elseif monte ==4
x = x - delta;
end
%--Applying BC if sin is btween pi and 2pi--%
%--Applying it by not allowing y >= 0--%
if y < 0
y = 0;
end
%--Calculating Theta for the BC--%
theta = atan(y./x);
%--Solving for radius to see if BC's are hit--%
radius = sqrt(x.^2 + y.^2);
if radius >=1
A = sin(theta);
elseif radius < 1
A = 0;
end
sol = A;
%--Plotted walk on a circle plot--%
subplot(211);
plot(x,y,'.b');
sol;
end
%--Plotting Surface of Solution--%
figure(2)
mesh(x,y,sol)
0 个评论
回答(2 个)
Walter Roberson
2014-3-27
You are initializing
y = zeros(1,n);
x = zeros(1,n);
which is good, but then in the code you have (for example)
y = y + delta;
which is going to affect all of y, not just the t'th y. You need to fix that.
Your x and y values that come out will be vectors that are scattered around, not a regular grid. You can only use mesh() when the x and y represent grids. If you want a gridded surface then you should look at triscatteredinterp() or the newer griddedinterpolant()
3 个评论
Walter Roberson
2014-3-27
If you want a gridded surface then you should look at triscatteredinterp() or the newer griddedinterpolant(). Or even griddata()
It has nothing to do with your original initialization of sol; that initialization is correct. The problem is that your points are not gridded: you have x(t), y(t), sol(t) as a single 3-space path, but mesh() needs a sol(i,j) for every possible x(i), y(j) pair.
What, by the way, do you want to do when the path returns to the same grid location?
Also have you considered using atan2(y, x) instead of atan(y/x) ?
RAGHAVENDRA
2014-3-27
In addition to the Walter Roberson’s advice. I would like to say that. As per your program at line 45: if y<0 y=0; end The resulting y will be a single value(1X1 vector) . This will result in an error. Hence you need to fix that also.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!