Nested if statements not correctly calculating?

I'm solving a dirichlet problem using the upwind method and finite differences. I'm trying to construct the system of equations that I need so that I can obtain the coefficients and solve the system for the solution. Below is my code.. the output I get does not seem to include the i=2,j=2 case.. and I seem to be missing some variables as well. I am brand new to MATLAB, any help is appreciated.
clear
clc;
close all
%Step size and grid points
syms x y f u a
h=0.25;
k=0.05;
xv=1:h:9;
yv=1:k:4;
n=length(xv);
m=length(yv);
%Boundary Conditions
f(x,y)=x^2-y^2;
b1=xv(1:n);
b2=yv(1:m);
u(1:n,1)=f(b1,1)
u(1:n,m)=f(b1,4)
u(1,1:m)=f(1,b2)
u(n,1:m)=f(9,b2)
% u(n,1:m) is a matrix with interior zeros (perimeter is filled by boundary conditions), here
% a is my attempt to define variables to assign to the elements in u(n,1:m) which are zero
a=sym('a',[n m]);
%Trying to calculate system of equations accounting for boundary conditions and elements
% which may be 0 that I wish to actually represent a variable (if the entry u(i,j) = 0 then
% I wish to assign this instead as u(i,j)=a(i,j) where a is a symbolic variable
for i=2:n-1
for j=2:m-1
if u(i,j)==0
if u(i+1,j)==0
if u(i,j+1)==0
if u(i-1,j)==0
if u(i,j-1)==0
u(i,j-1)=a(i,j-1);
u(i-1,j)=a(i-1,j);
u(i,j+1)=a(i,j+1);
u(i+1,j)=a(i+1,j);
u(i,j)=a(i,j);
end
end
end
end
end
C=(((1/h)+(0.1)*yv(j))*u(i+1,j)+...
((1/k)+(0.1)*xv(i))*u(i,j+1)-...
((2/h)+(2/k)+(0.1)*(xv(i)+yv(j)))*u(i,j)+((1/h)*u(i-1,j))+...
((1/k)*u(i,j-1)))
end
end
% These are the first few results I get from the output
C =
271/25
C =
-21/25
C =
-129/100
% It finally begins to construc equations but some of them seem to be missing variables, and
% the first equation is in terms of a(3,3), not a(2,2) which is what I want
C =
4*a2_3 + 20*a3_2 - (2413*a3_3)/50 + (403*a3_4)/20 + (411*a4_3)/100
C =
20*a3_3 - (9653*a3_4)/200
C =
20*a3_4
C =
4*a2_6 + 20*a3_5 - (1931*a3_6)/40 + (403*a3_7)/20 + (33*a4_6)/8

 Accepted Answer

if u(i,j)==0
if u(i+1,j)==0
if u(i,j+1)==0
if u(i-1,j)==0
if u(i,j-1)==0
u(i,j-1)=a(i,j-1);
end
u(i-1,j)=a(i-1,j);
end
u(i,j+1)=a(i,j+1);
end
u(i+1,j)=a(i+1,j);
end
u(i,j)=a(i,j);
end
Use following type. May be this is what you're want

3 Comments

You could use with
if condition1
...
elseif cond2
...
elseif
...
end
Which is better format
Thank you, that certainly was helpful! Also, I did try the elseif first but I may have used that in the wrong format as well.. however, a new problem has arisen. Here are the first few outputs now.. I see that a(3,3) is missing for example in the second output. I assume this is becuase the loop is fixed at i=2 and so a(3,3) is assigned a value of zero? Is that correct? Any hints on how I could rectify that if so?
C =
(161*a2_3)/8 - (4823*a2_2)/100 + (821*a3_2)/200 + 271/25
C =
20*a2_2 - (9647*a2_3)/200 - 21/25
C =
20*a2_3 - (1206*a2_4)/25 + (161*a2_5)/8 + (823*a3_4)/200 - 129/100
C =
20*a2_4 - (9649*a2_5)/200 - 44/25
C =
20*a2_5 - (193*a2_6)/4 + (161*a2_7)/8 + (33*a3_6)/8 - 9/4
I moved the nested if statements outside of the for loop and put it in its own for loop and that seems to have done the trick, although I'm not sure if it's the most efficient. Thank you very much for the help!

Sign in to comment.

More Answers (0)

Categories

Find more on Function Creation in Help Center and File Exchange

Asked:

on 17 Dec 2020

Commented:

on 17 Dec 2020

Community Treasure Hunt

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

Start Hunting!