solvepde fails on complex 'f' coefficient

5 views (last 30 days)
Avraham Klein
Avraham Klein on 20 Jul 2016
Commented: Hansu zhang on 17 Mar 2019
I am trying to solve a pde using complex coefficients. However I am running into the following issue. Whenever I run solvepde I get an error regarding the complex coefficients.
Here is a MWE:
model = createpde();
geometryFromEdges(model, @circleg);
applyBoundaryCondition(model, 'Edge', 1:model.Geometry.NumEdges, 'u', 0);
generateMesh(model);
fC = @(r, s) complex(1i*ones(1,numel(r.x)));
specifyCoefficients(model, 'm', 0, 'd', 0, 'a', 0, 'c', 1, 'f', fC);
solvepde(model)
Which gives me:
Error using formGlobalKF2D Coefficient evaluation function, "@(r,s)complex(1i*ones(1,numel(r.x)))", returned a complex matrix but previously returned a real matrix. This function must consistently return the same type of matrix.
Any help would be very welcome! Thank you!

Answers (2)

Ravi Kumar
Ravi Kumar on 26 Jun 2018
Hi Yauhen,
The expression
exp((r.x.^2+r.y.^2)/2).*exp(-1i.*s.time)
evaluates to real 1 when s.time =0, r.x= 0, r.y=0. Solver detects switching from real to complex or vice versa. To ensure the value of fC is consistent, please use the following expression for fC.
fC = @(r, s) complex(exp((r.x.^2+r.y.^2)/2).*exp(-1i.*s.time));
In this case fC = 1+0i when s.time =0, r.x= 0, r.y=0. Thus keeping it consistent throughout the solution.
Regards,
Ravi
  2 Comments
Yauhen
Yauhen on 26 Jun 2018
Finally it works!
Thank you very much.
Yauhen
Hansu zhang
Hansu zhang on 17 Mar 2019
Thank you Ravi Kumar
My matlab error "returned a complex matrix but previously returned a real matrix. This function must consistently ret". Your expression for fC works well . (ง •̀_•́)ง

Sign in to comment.


Alan Weiss
Alan Weiss on 21 Jul 2016
It seems to me that your problem would be best solved by setting the f coefficient to 1i, a constant rather than a function. However, when I tried that, it did not work in solvepde. It is possible that there is a bug in solvepde; the development team is investigating.
Meanwhile, you might do better to use the legacy workflow using assempde.
Sorry, that is all I have at the moment.
Alan Weiss
MATLAB mathematical toolbox documentation
  8 Comments
Yauhen
Yauhen on 26 Jun 2018
Hi Ravi Kumar, Thank you for fast reply, here is an example of the code:
model = createpde();
geometryFromEdges(model, @circleg);
applyBoundaryCondition(model, 'Edge', 1:model.Geometry.NumEdges, 'u', 0);
generateMesh(model,'Hmax',0.1);
fC = @(r, s) exp((r.x.^2+r.y.^2)/2).*exp(-1i.*s.time);
dC = -1i;
specifyCoefficients(model, 'm', 0, 'd',dC, 'a', 0, 'c', 1, 'f', fC);
setInitialConditions(model,0);
nframes = 30;
tlist = linspace(0,0.1,nframes);
result = solvepde(model,tlist)
u = result.NodalSolution;
The errors are:
Error using formGlobalKF2D
Coefficient evaluation function, "@(r,s)exp((r.x.^2+r.y.^2)/2).*exp(-1i.*s.time)", returned a complex matrix
but previously returned a real matrix. This function must consistently return the same type of matrix.
Error in pde.DiscretizedPDEModel/getStationaryFEMatrices (line 80)
[K, F] = formGlobalKF2D(self.emptyPDEModel, self.p, self.t, self.coefstruct,u,time);
Error in pde.DynamicDiscretizedPDEModel/getDynamicFEMatrices (line 118)
[K,A,F,Q,G,H,R] = self.getStationaryFEMatrices(u,time);
Error in pde.DynamicDiscretizedPDEModel/checkSpatialCoefsForUorTDependence (line 52)
[Mass0,K0,A0,F0,Q0,G0,H0,R0] = self.getDynamicFEMatrices(u0, t0);
Error in pde.DynamicDiscretizedPDEModel (line 38)
obj = obj.checkSpatialCoefsForUorTDependence(u0,tlist);
Error in pde.EquationModel/solveTimeDependent (line 25)
femodel=pde.DynamicDiscretizedPDEModel(self,p,e,t,coefstruct,u0,tlist,tsecondOrder);
Error in pde.PDEModel/solvepde (line 54)
[u,dudt] = self.solveTimeDependent(coefstruct, u0, ut0, tlist, ...
Do you have any ideas what is wrong here?
Best regards
Ravi Kumar
Ravi Kumar on 26 Jun 2018
I posted an answer that fixes the error.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!