MATLAB Answers

How to implement polyval in more than one dimension when the summation includes complex coefficients

1 view (last 30 days)
Matthew Kehoe
Matthew Kehoe on 10 Feb 2020
Answered: David Goodmanson on 10 Feb 2020
My problem involves the following double summation
where are complex coefficients, and are positive constants. I think that I can apply the PolyVal2D function written by Mark Mikofski. However, as my coefficients are complex, I don't think the function will work with my code.
My first attempt involved truncating a double summation after computing a specified number of orders in the taylor expansion. In order to do this. I created the function below.
function val2 = test(unm)
Eps = 1e-6; Delta = 1e-4; Nx = 48; Nz = 32; N = 8; M = 8;
coeff = zeros(N+1,M+1);
u_approx = zeros(Nx,Nz+1);
for j=1:Nx
for ell=0:Nz
coeff(:,:) = unm(j,ell+1,:,:); % Returns a 2D array of dimension (N+1) x (M+1) of complex doubles
u_approx(j,ell+1)=taylorsum_2(coeff,Delta,Eps,N,M);
end
end
val2 = u_approx;
return;
(My code for how I form is omitted. I could provide the values of if necessary, it is a 2D array of complex doubles of dimension )
The above function references the function taylor_sum2
function [tsum] = taylorsum_2(c,Eps,Delta,N,M)
% taylorsum - Sums a truncated Taylor series.
%
% Inputs:
%
% c - Taylor series coefficients: [c_00,...,c_0N] ... [c_M0,...,c_MN] which are a 2D array of complex doubles of dimension (N+1) x (M+1)
% Eps - Value at which to sum for N
% Delta - Value at which to sum for M
% N - Degree of truncated Taylor series
% M - Degree of truncated Taylor series
%
% Outputs:
%
% tsum - Taylor sum evaluated at Eps and Delta
tsum = polyVal2D(c,Eps,Delta,N,M);
which then references the code written in polyVal2D. At line 31 of polyVal2D, code is written to check the input arguments of the coeffcients. It appears that the coefficients must be real for this code to work (my own testing returned odd results when this restriction was removed - all of the coefficients returned are greater than !)
%% check input args
validateattributes(p,{'numeric'},{'2d','nonempty','real','finite'}, ...
'polyVal2D','p',1)
validateattributes(x,{'numeric'},{'nonempty','real','finite'}, ...
'polyVal2D','x',2)
validateattributes(y,{'numeric'},{'nonempty','real','finite'}, ...
'polyVal2D','y',3)
assert(all(size(x)==size(y)),'polyVal2D:sizeMismatch', ...
'X and Y must be the same size.')
Through this analysis, I have the following questions:
  1. Does the above function ( polyVal2D) work with a 2D array of complex doubles for coefficients? I emailed the author since I was unsure about expected functionality. I'm not sure if I could adjust the code inside polyVal2D so that it will work with my 2D array of complex doubles.
  2. If that is the case, I think that my other option is shown inside the answers to this_question. The polyfit_n function was created to handle scenarios similar to mine. I don't know how to implement it, so I have instead tried to implement the polyVal2D function.

  2 Comments

David Goodmanson
David Goodmanson on 10 Feb 2020
Hi Matthew,
are you talking about polyval, which is just a straightforward sum with known unm, or are you talking about polyfit, where you ar tryng to find the unm that are a best fit to a known set of values?
Matthew Kehoe
Matthew Kehoe on 10 Feb 2020
Hello David,
I am looking at polyval or the sum of the known u_nm. I have the values of the u_nm through an external function and am applying polyval on the known values of the coefficients.

Sign in to comment.

Answers (1)

David Goodmanson
David Goodmanson on 10 Feb 2020
Hi Matthew,
unless I'm missing something,
% make up some data
M = 5;
N = 6;
c = rand(N,M)+i*rand(N,M);
eps = .3;
delt = .4;
for n = 1:N
for m = 1:M
polyv(n,m) = c(n,m)*eps^n*delt^m;
end
end
polyv
or in place of the for loops,create a vandermonde-like matrix
[nval mval] = ndgrid(1:N,1:M);
vander = eps.^nval.*delt.^mval
polyv1 = c.*vander;
polyv1
polyv1 - polyv % should be tiny
Your values of eps and delta are small enough that you can hardly see the effect, so they are a lot larger in this example.

  0 Comments

Sign in to comment.

Sign in to answer this question.