필터 지우기
필터 지우기

How to create a matrix from a for loop answer?

조회 수: 1 (최근 30일)
Mike
Mike 2014년 3월 27일
댓글: Mike 2014년 3월 27일
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)

답변 (2개)

Walter Roberson
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
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) ?
Mike
Mike 2014년 3월 27일
No i haven't considering using atan2 vs atan. To be honest i'm still learning MATLAB so some of this stuff isn't so obvious.
I will try using a gridded surface in place of the meshgrid.
Also i'm not sure i understand the question "what do you want to do when the path returns to the same grid location?" Could you elaborate?

댓글을 달려면 로그인하십시오.


RAGHAVENDRA
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.
  댓글 수: 1
Mike
Mike 2014년 3월 27일
How would you recommend that being fixed?
I was trying to satisfy a boundary condition with that line. However i think my biggest problem in all of this was the code:
%--Plotting Surface of Solution--%
figure(2)
mesh(x,y,sol)
Those x,y values are supposed to be capitalized rather than smaller case. I just need X and Y to be a small range and not the values i was computing before. Hence why i wrote:
[X,Y] = meshgrid(-1:0.1:1);
Think by changing all of that i would still have an issue?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by