How can I treat a symbolic function in this script related to lagrangian interpolation
15 views (last 30 days)
Show older comments
Ricardo Boza Villar
on 17 May 2016
Commented: Bobby Fischer
on 10 Apr 2022
I have described what the script is intended to do with commentary (time spent to solve the problem 15~20 minutes max, I promise ;)).
The problem that I encounter is the following:
It says 'line 39'; I don't know how I can create the symbolic function in order to get the program running.
I wouldn't say it's really difficult and complicated. I think the difficulty lies in trying to understand what I have described. I've done it as thoroughly as I could, but you can ask me if you want some more information.
% This function is intended to calculate the Lagrange polynomial that
% interpolates a one-variable function given by the user as input.
function []=a75lagrange(f,nodes)
% f : f is the function that the user wants to interpolate.
% nodes: nodes is a vector containing the nodes of interpolation, that is,
% the 'xvalues' of which the user has information because supposedly
% the user hasn't had access to any more of them. This may be caused by
% the fact that evaluating the function is very difficult or simply
% because the user doesn't have its expression.
% For example, I would write on the workspace:
% 'a75lagrange(@cos,[0,1/2,2,3])'
format compact, format shortg
% This format is practical, though it doesn't serve a particular purpose
% because the function doesn't have output.
syms x
% 'syms x' creates the symbolic variable 'x'
polynomial=0;
% I start the polynomial at zero because I want to define the variable
% before it enters de loop.
lon=length(nodes);
% How many nodes there are as input.
% 'lon' because in Spanish 'length' is 'longitud'.
for m=1:lon
% For example, if we have 4 nodes, we would have 4 terms L_1,L_2,L_3
% and L_4, and all of them will depend on x, the symbolic variable.
L(m)=1;
% I define each of them before they enter their loops.
yvalue(m)=f(nodes(m));
% This is the y value that the function takes when we evaluate it on
% its respective nodes.
for k=1:lon
% This loop is to develop the shape that each L_1,L_2,L_3 and L_4
% is going to take.
if m~=k
% m~=k is a condition given by the algorithm.
% If it wasn't this way one denominator below would cancel
% itself.
L(m)=L(m)*(x-nodes(k))/(nodes(m)-nodes(k));
end
% When exiting the loop we have 1 of the 4 expressions that
% we wanted at the beginning, corresponding to 1 Lagrangian term of
% the final interpolating polynomial.
polynomial=polynomial+yvalue(m)*L(m);
% After each loop, the polynomial takes with it another expression
% that contributes to its final shape.
end
end
s=(nodes(1)-5:0.01:nodes(end)+5);
a=axis;
% s is a vector of x values. My intention is to evaluate each of the
% Lagrangian terms separately, and plot them using subplot.
% Then, opening another figure, I would get the second graph, in which the
% real function and the whole interpolating polynomial would appear.
figure(1),close(1),figure(1)
hold on
for n=1:lon
q=sym2poly(L(m));
% As L(m) was a symbolic mathematical expression depending on x, using
% this I would get a vector of coefficients that MATLAB would give me.
y=polyval(q,s);
% I would use the command 'polyval' to evaluate that polynomial given
% by the vector of coefficients.
subplot(2,2,n)
plot(a(1:2),[0,0],':'), plot([0,0],a(3:4),':')
% With these I get the axes for the plots.
plot(nodes,zeros(size(nodes)),'o')
% With this I get the nodes.
plot(nodes(n),yvalue(n),'*')
% With this I get the y value of the n_th node.
plot(s,y)
% With this I get the graph of each of the Lagrangian terms.
end
hold off
figure(2),close(2),figure(2)
hold on
r=sym2poly(polynomial);
% As 'polynomial' was a symbolic expression, using sym2poly I get the
% coefficients of the polynomial.
yr=polyval(r,s);
% I evaluate that polynomial using the vector 's' defined earlier.
z=f(s);
% I get the y values for the real function.
plot(a(1:2),[0,0],':'), plot([0,0],a(3:4),':')
% With these I get the axes.
plot(nodes,zeros(size(nodes)),'o')
% With this I get the nodes.
plot(nodes,yvalue,'*')
% With this I get the y values of the nodes.
plot(s,z)
% With this I get the graph of the function given as input.
plot(s,yr,'r--')
% With this I get the graph of the interpolating polymial.
hold off
xlabel('Eje x')
ylabel('Eje y')
0 Comments
Accepted Answer
Walter Roberson
on 17 May 2016
L(m) = sym(1);
Remember the datatype is determined by the assignment, so by assigning plain 1 you were making the data type double rather than symbolic.
0 Comments
More Answers (2)
Ricardo Boza Villar
on 20 May 2016
Edited: Ricardo Boza Villar
on 20 May 2016
3 Comments
Bobby Fischer
on 10 Apr 2022
Thanks guys. (I'm Ricardo (Boza, not Milos)). This reminds me of a sad moment in my life. But anyway... The important thing is to move on, stronger than before. Still alone, still alive, still unbroken.
See Also
Categories
Find more on Get Started with Symbolic Math Toolbox 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!