How do I avoid using if and else statements in this code

I want to rewrite this code but without if and else statements. I just want to enhance convergence rate
for i=1:N-1
for j=1:M-1
A(i+(M-1-j)*(N-1),i+(M-1-j)*(N-1))=-2/dx^2-2/dy^2;
B(i+(M-1-j)*(N-1))=B(i+(M-1-j)*(N-1))-(cos(x(i)+y(j))+cos(x(i)-y(j)));
if i==1
B(i+(M-1-j)*(N-1))=B(i+(M-1-j)*(N-1))-cos(y(j))/dx^2;
else
A(i+(M-1-j)*(N-1),i-1+(M-1-j)*(N-1))=1/dx^2;
end
end
end

3 Comments

Why do you want to remove the if? What is your end goal? To speed up the code?
Also, please use the layout tools to make your code more readable.
Yes, I want to speed up the code
Can you write down the equation you are trying to implement with these for loops?

Sign in to comment.

 Accepted Answer

Hi,
If your main goal is to remove the if else statements from the code, you can write the following code first and then write the main for loop with i starting from 2 instead on one and remove the if statement and keep the statement in the else condition.
i = 1;
for j=1:M-1
A(i+(M-1-j)*(N-1),i+(M-1-j)*(N-1))=-2/dx^2-2/dy^2;
B(i+(M-1-j)*(N-1))=B(i+(M-1-j)*(N-1))-cos(y(j))/dx^2;
end
for i=2:N-1
for j=1:M-1
A(i+(M-1-j)*(N-1),i+(M-1-j)*(N-1))=-2/dx^2-2/dy^2;
B(i+(M-1-j)*(N-1))=B(i+(M-1-j)*(N-1))-(cos(x(i)+y(j))+cos(x(i)-y(j)));
A(i+(M-1-j)*(N-1),i-1+(M-1-j)*(N-1))=1/dx^2;
end
end
if you want to further optimize you can vectorize the first loop.
Hope this helps!

6 Comments

I trying to capture another block of code inside the one above but I am not getting the right results.
I want to include
if i==N-1
B(i+(M-1-j)*(N-1))=B(i+(M-1-j)*(N-1))+cos(y(j))/dx^2;
else
A(i+(M-1-j)*(N-1),i+1+(M-1-j)*(N-1))=1/dx^2;
end
in your code and I am using what is below but my answers aren't correct. I don't know what I am doing wrong exactly.
i=1;
for j=1:M-1
A(i+(M-1-j)*(N-1),i+(M-1-j)*(N-1))=-2.0/dx^2-2.0/dy^2;
B(i+(M-1-j)*(N-1))=B(i+(M-1-j)*(N-1))-cos(y(j))/dx^2;
end
i=N-1;
for j=1:M-1
A(i+(M-1-j)*(N-1),i+(M-1-j)*(N-1))=-2.0/dx^2-2.0/dy^2;
B(i+(M-1-j)*(N-1))=B(i+(M-1-j)*(N-1))+cos(y(j))/dx^2;
end
for i=2:N-2
for j=1:M-1
A(i+(M-1-j)*(N-1),i+(M-1-j)*(N-1))=-2.0/dx^2-2.0/dy^2;
B(i+(M-1-j)*(N-1))=B(i+(M-1-j)*(N-1))-(cos(x(i)+y(j))+cos(x(i)-y(j)));
A(i+(M-1-j)*(N-1),i-1+(M-1-j)*(N-1))=1/dx^2;
A(i+(M-1-j)*(N-1),i+1+(M-1-j)*(N-1))=1/dx^2;
end
end
I would still second the advice from Ameer: post the equations you want to solve. A change of algorithm can increase calculation many times, while optimization can only get you an order of magnitude.
Speaking of optimization: did you pre-allocate A and B?
SA
SA on 7 May 2020
Edited: SA on 7 May 2020
Hi Rik,
I am actually solving a differential equation using a difference algorithm and the only equations i was given are
I am using the algorithm to create the equations above but I want to solve the problem without if and else statements
I wouldn't personally care how my code would solve the equations. The more time you put in, the more you can optimize your code. At some point you get to a stage where you need to put in an unreasonable amount of time. Then I would stop.
Are you really sure you simply want to avoid an if/else block? That doesn't make sense to me.
What toolboxes relating to diff equations do you have available?
Hi Rik,
Yes, I just want to avoid the if/else block. I want to solve the pde another way (without using the if/else block).
I am not using an toolboxes in matlab. I just want to solve the pde using the difference algorithm.

Sign in to comment.

More Answers (0)

Asked:

SA
on 1 May 2020

Commented:

SA
on 7 May 2020

Community Treasure Hunt

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

Start Hunting!