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)*c​os(w1*(x-x​(i))))?

1 vue (au cours des 30 derniers jours)
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 commentaires
Dr. Anju Chaurasia
Dr. Anju Chaurasia le 28 Jan 2022
Modifié(e) : Dr. Anju Chaurasia le 29 Jan 2022
@ John, you are right. I want different polynomials for diffrent value of i. Fi(x) for i=1,2,...n. Here points x1, x2,.....,xn are not equally spaced.
John D'Errico
John D'Errico le 29 Jan 2022
Modifié(e) : John D'Errico le 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?

Connectez-vous pour commenter.

Réponse acceptée

VBBV
VBBV le 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 commentaire
Dr. Anju Chaurasia
Dr. Anju Chaurasia le 29 Jan 2022
Modifié(e) : Dr. Anju Chaurasia le 29 Jan 2022
Thanks! But what , if we have to substitute ci's i=1,2,..,5 from this matix solution? Not able to substitute. Getting error.
a(i)=[1 0 1 0 1
0 w1 0 w2 0
0 -w1^3 0 -w2^3 0
1 sin(w1*h(i)) cos(w2*h(i)) sin(w2*h(i)) cos(w1*h(i))
0 -w1^3*cos(w1*h(i)) w2^3*sin(w2*h(i)) -w2^3*cos(w2*h(i)) w1^3*sin(w1*h(i))];
end
b(i)= [S(i) D(i) F(i) S(i+1) F(i+1)]'
C=[sym(a(i)\b(i))]; and
c1(i)=C(1);
c2 (i)=C(2);
c3(i)=C(3);
c4(i)=C(4);
c5(i)=C(5);

Connectez-vous pour commenter.

Plus de réponses (2)

KSSV
KSSV le 27 Jan 2022
Modifié(e) : KSSV le 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 commentaires
John D'Errico
John D'Errico le 27 Jan 2022
Modifié(e) : John D'Errico le 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
KSSV le 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.

Connectez-vous pour commenter.


John D'Errico
John D'Errico le 29 Jan 2022
Modifié(e) : John D'Errico le 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 commentaire
Dr. Anju Chaurasia
Dr. Anju Chaurasia le 31 Jan 2022
Modifié(e) : Dr. Anju Chaurasia le 31 Jan 2022
@John D'Errico, how can you say the chosen spline is not continous? This is the problem of two parameters. Please, help to run code. It's not working, when I put the value of C.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by