Coding in m-file, for a func defined in an interval [x0, xn] like Fi(x)(=(c1(i)+c2(i)*sin(w1*(x-x(i)))+c3(i)*cos(w2*(x-x(i)))+c4(i)*sin(w2*(x-x(i)))+c5(i)*cos(w1*(x-x(i))))?
1 view (last 30 days)
Show older comments
Dr. Anju Chaurasia
on 27 Jan 2022
Edited: Dr. Anju Chaurasia
on 31 Jan 2022
Hi,
I want to write coding for a function which is defined at a no. of point and after that differentiate it. where x(i)=x(1)+i*h(i) and h(i) = x(i+1)-x(i). i=1,...n., ci's are constants.
6 Comments
John D'Errico
on 29 Jan 2022
Edited: John D'Errico
on 29 Jan 2022
There are still some issues with the question. Note that it is a REALLY bad idea to use the vector x(i) to indicate break points along the real line that denotes x. That means you have expressions like x-x(i), something that will be confusing as hell in any code you write. It will make your code difficult to debug when that becomes necessary. I would probably call the vector of break points something like xbreak, or perhaps xb for brevity. So now you would see something like x-xb(i). Readable code is code that becomes far easier to debug.
If this is intended to be effectively a spline but not a classical spline, instead one using trig functions of x, how will you insure the function is now continuous, and differentiable in any way? That must be written in the form of constraints on the vector variables c1, c2, c3, c4, c5. Or do you not care about continuity?
Accepted Answer
VBBV
on 29 Jan 2022
% define c1, c2, c2, c4, c5 and W1, W2 etc
c1 =1;
c2 = 1.5;
c3 = 4;
c4 = 4.75;
c5 =5;
w1 = 2.3;
w2 = 5;
n = 100 ;
x0 = 1 ;
xn = 10 ;
x = linspace(x0,xn,n) ;
F = zeros(n-1,n) ;
for i = 1:n-1
h(i)= x(i+1)-x(i); % this is indeed strange
x(i) =x0+i*h(i);
F(i,:)=(c1+c2*sin(w1*(x-x(i)))+c3*cos(w2*(x-x(i)))+c4*sin(w2*(x-x(i)))+c5*cos(w1*(x-x(i))));
end
plot(x,F(1:20:end,:))
As @Torsten mentioned, the strange part of your equations is x(i) dependence on x(i+1). Recheck your equations,
1 Comment
More Answers (2)
KSSV
on 27 Jan 2022
Edited: KSSV
on 28 Jan 2022
% define c1, c2, c2, c4, c5 and W1, W2 etc
n = 100 ;
x0 = 1 ;
xn = 10 ;
x = linspace(x0,xn,n) ;
F = zeros(n-1,1) ;
for i = 1:n-1
F(i)=(c1+c2*sin(w1*(x(i+1)-x(i)))+c3*cos(w2*(x(i+1)-x(i)))+c4*sin(w2*(x(i+1)-x(i)))+c5*cos(w1*(x(i+1)-x(i)))) ;
end
You can also achieve the same without loop.
3 Comments
John D'Errico
on 27 Jan 2022
Edited: John D'Errico
on 27 Jan 2022
@KSSV - You are mistaken. You have written terms like this: x(x+1)-x(i)), which is not what was shown in the expression. What I see written are things like (x - x(i)), which is very different from what you wrote. At the same time, the question is confusing.
KSSV
on 28 Jan 2022
@John D'Errico Yes the question is confusing. May be OP wants to use symbolic toolbox to evaluate and keep x as variable. I felt OP is in the begining stage and asked a silly question. YEs, I felt writitng x(i+1)-x(i) is not the coreect way.
John D'Errico
on 29 Jan 2022
Edited: John D'Errico
on 29 Jan 2022
There are still some issues with the question. Note that it is a REALLY bad idea to use the vector x(i) to indicate break points along the real line that denotes x. That means you have expressions like x-x(i), something that will be confusing as hell in any code you write. It will make your code difficult to debug when that becomes necessary. I would probably call the vector of break points something like xbreak, or perhaps xb for brevity. So now you would see something like x-xb(i). Readable code is code that becomes far easier to debug.
Next, I'll assume w1 and w2 are scalars, since you do not show an index for them.
If this is intended to be effectively a spline but not a classical spline, instead one using trig functions of x, how will you insure the function is now continuous, and differentiable in any way? That must be written in the form of constraints on the vector variables c1, c2, c3, c4, c5. Or do you not care about continuity?
My code to EVALUATE the function in the form of a spline might look vaguely like:
function y = trigspline(x,xb,C,w1,w2)
% x is any scalor or vector of values for x
% C is a nx5 array of coefficients. So the vector c1 will be C(:,1)
% w1 and w2 are scalars to be provided.
%
% As returned, y will be an array of the same shape and size as x.
% determine the number of intervals
n = length(xb);
% preallocate a result for y, as
y = zeros(size(x));
% it may be easier to write the function if we unpack the array C into
% vectors, at least it will be easier to write the function evaluation
% itself.
c1 = C(:,1);
c2 = C(:,2);
c3 = C(:,3);
c4 = C(:,4);
c5 = C(:,5);
% for each element of x, which break point interval does x lie in?
% this will best done using the function discretize.
i = discretize(x,xb);
% note that if x is a vector or array of unknowns, then so will be i.
% now just write the function evaluation in a vectorized form. That means
% to use .* for a vectorized, element-wise multiplication. since w1 and
% w2 appear to be scalars, they can use a simple * for multiplication.
y = c1(i)+c2(i).*sin(w1*(x-xb(i)))+c3(i).*cos(w2*(x-xb(i)))+c4(i).*sin(w2*(x-xb(i)))+c5(i).*cos(w1*(x-xb(i)));
end
I have not test the above code. It assumes you wrote that line of code correctly to evaluate the function. I've not carefully checked the parens to be sure they are correct, but it looks ok at a glance.
There are still issues in the use of that code, in terms of how you will compute the unknowns w1,w2, and the array of coefficients C, such that the above function will produce a well defined, hopefully continuous and differentiable function of the variable x. A problem is that while C can be estimated using linear systems of equations (perhaps a tool like lsqlin, which can help with the constraints for continuity and differentiability), if you would also try to solve for w1 and w2, the problem becomes nonlinear.
1 Comment
See Also
Categories
Find more on Splines in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!