# Algorithmic differentiation in unsteady heat equation

• Falgata
In summary, the conversation is about implementing an optimizer into a heat equation primer code and facing issues with the cost function. The speaker suggests double checking the differentiation process and adjusting parameters in the cost function as potential solutions.
Falgata

## Homework Statement

Okey her we go
I was given a base code called heat_equation_primer. The goal is to implement a optimizer into the program. The two methods that are going to be used is the Quasi-Newton and Steepest descent with search line. So I need gradients. So I tried to differentiate as best I could. Then using forward AD I was trying to verify what weighting the different values of a had on the system. The target values was given by the professor. The problems is that mine are to high and by a gargantuan size as well being 1 order of magnitude higher. He gave a suggestion where to start. I started in the set_bc and followed where the variables designed there headed. The differential will be makred with a d after the variable.

## Homework Equations

The problem I think might be in the cost function L
L=sqrt(1/(4N) sum from 1 to 4N (fi-f(T,i))
noted in the code as L=sqrt(L/MX)

## The Attempt at a Solution

Here is the parts modified code. I am not looking for someone to solve this for me Just to point me into the right direction. Thanks

Matlab:
function Tbot = T_bottom ( x, a )
[nr, MDVAR] = size(a) ;
Tbot = 0.0 ;
phi = x*pi ;
for n = 1:MDVAR/2
arg = n*phi ;
Tbot = Tbot + a(2*n-1)*cos(arg);
Tbot = Tbot + a(2*n)  *sin(arg) ;
end
end
function [T, Td] = set_bc ( T, Td, a, ad )
for k = LEFT+1:RIGHT-1
x = get_x ( k ) ;
T(k,BOT) = T_bottom( x, a );
T(k,TOP) = 0.0 ;
Td(k, TOP) = 0.0;
end
for l=BOT+1:TOP-1
T(LEFT,l) = T(LEFT+1,l) ;
Td(LEFT,1) = Td(LEFT+1,1);
T(RIGHT,l) = T(RIGHT-1,l) ;
Td(RIGHT,l) = Td(RIGHT-1,l) ;
end
end
function [res, resd, resNorm, resNormd] = residual ( T, Td )
resNorm = 0.0 ;
res = 0.0 ;
resd = 0.0;
resNormd = 0.0;
for k=LEFT+1:RIGHT-1
for l = BOT+1:TOP-1
r = T(k-1,l) - 2*T(k,l) + T(k+1,l) + T(k,l-1) - 2*T(k,l) + T(k,l+1) ;
rd = Td(k-1,l) - 2*Td(k,l) + Td(k+1,l) + Td(k,l-1) - 2*Td(k,l) + Td(k,l+1);
res(k,l) = r ;
resd(k,l)= rd;
resNorm =resNorm+ r^2 ;
resNormd=resNormd+ 2*r*rd;
end
end
end
function [T, Td] = update_explicit ( T, Td, res, resd, cfl )
for k=LEFT+1:RIGHT-1
for l = BOT+1:TOP-1
T(k,l) = T(k,l) + cfl*res(k,l) ;
Td(k,l) = Td(k,l)+cfl*resd(k,l);
end
end
end
function [L, Ld] = cost ( T, Td )
L = 0 ;
Ld = 0;
for k = LEFT+1:RIGHT-1
x = get_x ( k ) ;
if ( x < -0.5 )
fTarget = 0 ;
elseif ( x < 0.0 )
fTarget = 1 ;
else
fTarget = 0 ;
end
f = ( T(k,TOP)-T(k,TOP-1) )/DY ;%% actual flux between internal node and ghost node at i.
fd = (Td(k,TOP)-Td(k,TOP-1))/DY;
df = fTarget-f ;%% difference to target.
dfd =-fd;
L = L + df^2 ;%% norm.
Ld = Ld + 2*df*dfd;
end
L = sqrt(L/MX) ;
Ld = (0.5*Ld*sqrt(1/MX))/sqrt(L);
end

for i=1:MDVAR
fprintf ( 'variable %d\n', i ) ;
[T, Td] = init () ;
[L,Ld, T, Td] = heatEq_solve_explicit ( T, Td, cfl, a, ad, tol, maxIter, fprintfreq );
v(i)=Ld;
fprintf ( '       a = %g\n', v(i)) ;
end
end
<Moderator's note: please use code tags>

Last edited by a moderator:

Hello there,

It seems like you are trying to implement an optimizer into a heat equation primer code. Your goal is to use the Quasi-Newton and Steepest descent with search line methods and you need gradients for this. However, you are experiencing some issues with the cost function, where your values are much higher than the target values given by your professor.

From the code you have provided, it looks like you have already made some modifications to the original code. It would be helpful if you could provide more context and explanation for your code, so that I can better understand your approach and suggest potential solutions.

One thing I would suggest is to double check your differentiation process and make sure that it is correct. You can also try plotting your results to see if there are any obvious discrepancies. Additionally, you can try adjusting the parameters in your cost function to see if that helps in getting closer to the target values.

I hope this helps and good luck with your project!

## 1. What is algorithmic differentiation?

Algorithmic differentiation is a mathematical technique used to automatically evaluate the derivatives of a function. It involves decomposing a function into elementary operations and applying the chain rule to calculate the derivatives of each operation. This enables more accurate and efficient evaluation of derivatives compared to traditional methods such as finite differences.

## 2. How does algorithmic differentiation work in the context of the unsteady heat equation?

In the unsteady heat equation, algorithmic differentiation involves breaking down the equation into its individual operations, such as addition, multiplication, and differentiation. The derivatives of each operation are then calculated using the chain rule, resulting in a more accurate and efficient method for solving the equation.

## 3. What are the benefits of using algorithmic differentiation in the unsteady heat equation?

Algorithmic differentiation offers several benefits in the context of the unsteady heat equation. It allows for more accurate and efficient evaluation of derivatives, which can lead to more accurate solutions. It also reduces the computational cost compared to traditional methods such as finite differences, making it a valuable tool for solving complex problems.

## 4. Are there any limitations to using algorithmic differentiation in the unsteady heat equation?

While algorithmic differentiation is a powerful tool, it does have some limitations. It may not be suitable for problems with discontinuities or singularities, as these can cause issues with the chain rule calculations. Additionally, it may be more challenging to implement compared to traditional methods, and it requires a good understanding of the underlying mathematics.

## 5. How is algorithmic differentiation used in practical applications of the unsteady heat equation?

In practical applications, algorithmic differentiation can be used to solve complex problems in fields such as engineering, physics, and finance. It allows for more accurate and efficient simulations of heat transfer, which can be beneficial in designing and optimizing various systems. It is also used in sensitivity analysis and uncertainty quantification, providing valuable insights into the behavior of the system.

• Engineering and Comp Sci Homework Help
Replies
9
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
• Atomic and Condensed Matter
Replies
7
Views
356
• Engineering and Comp Sci Homework Help
Replies
1
Views
1K
• Engineering and Comp Sci Homework Help
Replies
1
Views
1K
• Introductory Physics Homework Help
Replies
11
Views
245
• Engineering and Comp Sci Homework Help
Replies
3
Views
1K
• Set Theory, Logic, Probability, Statistics
Replies
16
Views
4K
• Special and General Relativity
Replies
1
Views
445
• Engineering and Comp Sci Homework Help
Replies
1
Views
1K