Clear Filters
Clear Filters

Matlab function to calculate coeffiecients of projection of a function on orthogonal set

1 view (last 30 days)
I want to write a function in matlab, that would do the following:
Given linear space of periodic functions, and given an orthogonal set in this space, I want to write a function that accepts as an argument a periodic function and the orthogonal set, and calculates the projection of the periodic function on each of the functions in the orthogonal set.
i.e, the arguments of the function are:
1)Vector which contains values of a periodic function
2)Matrix with N columns such that each column contains values of one of the orthogonal functions
3)Scalar - T which represents the period.
And the function should return a vector which contains the projections of the periodic function on each of the orthogonal functions. Meaning, the vector should return the coefficients .
Here's what I have tried:
function [coeff] = mekadmim_bekef(xt, M, T)
Size = size(M); %Get dimensions of M
num_of_samples=Size(2); %get the number of samples of each function
N=Size(2); %Get number of columns of M
coeff = zeros(1,N); %Initialize vector of length N
time_vector = 0:num_of_samples:T;
for k=1:N
curr_column = M(:,k);
conj_column=conj(curr_column)
num_to_int = xt.*conj_column;
deno_to_int = curr_column.*conj_column;
numerator = trapz(time_vector,num_to_int);
denominator = trapz(time_vector,deno_to_int);
coeff(k)=numerator./denominator;
end
But when I try to test it with the following code:
clc;
clear all;
close all;
t=0:2*pi/1000:2*pi;
f1 = sin(t);
f2=sin(2*t);
f3=sin(3*t);
f4=sin(4*t);
M=[f1;f2;f3;f4]';
T=2*pi;
a=mekadmim_bekef(f1',M,T);
disp(a);
It dosent work.
What went wrong?
Thanks in advance.

Answers (1)

Binaya
Binaya on 11 Oct 2023
Hi Itzik,
As per my understanding, you would like to understand why your code is not working for the required task.
While going through the code, I found following 2 errors in the given code:
  1. The function code snippet needs one more “end” command at the function.
  2. At line number 6 of the function, “time_vector” array is not correct. There are two methods to generate an array between two points with a fixed step:
a. time_vector = 0:step_size:T; where step_size = 2*pi/1000;
b. time_vector = linspace(0,T,1001);
Please refer to the below documentation for more details:
  1. Linspace: https://www.mathworks.com/help/matlab/ref/linspace.html
  2. Colon,”:”: https://www.mathworks.com/help/matlab/ref/colon.html
  3. Trapz: https://www.mathworks.com/help/matlab/ref/trapz.html
I hope this solves your query.
Regards
Binaya

Tags

Community Treasure Hunt

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

Start Hunting!