Nested if statements not correctly calculating?
Show older comments
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
More Answers (0)
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!