Plot a curve with changing slope and given initial and final end points?

11 views (last 30 days)
I am trying to plot a curve where slope changes and I have an array of slope values. If I have starting and ending points, can I find the plot? I am trying this code:
C = [1.638478564;1.63173352;1.619642865;1.607678048;1.595836914;1.572517306;1.548371795;1.526000781;1.482560794;1.439925437;1.35899495;1.292079231;1.228151661;1.167180043;1.109571502;1.002674932;0.904196956;0.824735887;0.749771126;0.678467841;0.610473737;0.577351902]; % Slopes array
y=zeroes(1,23);
x=zeroes(1,23);
y(1)=1;
x(1)=cot(0.7046);
y(23)=3*cos(0.5236);
x(23)=3*sin(0.5236);
for i = 1:22
(y(i+1)-y(i))/(x(i+1)-x(i))=C(i);
end
disp(y);
disp(x);
I am getting parse error at the equation involving curve.

Answers (1)

Roger Stafford
Roger Stafford on 3 May 2013
As you have posed the problem, no solution with monotone increasing x is possible, because the overall slope between the points (x1,y1) and (x23,y23) would have to be (y23-y1)/(x23-x1) = 4.93560164127029, which is greater than any of the slopes given in your "slope" array.
Assuming the values in (x1,y1) and (x23,y23) have been corrected to some appropriate values, your problem then becomes severely underdetermined. If everything is expressed in terms of the twenty-one unknowns x(2:22), (as it can be,) you would have only one equation and twenty-one unknowns. To arrive at a unique solution, you need to add further conditions to your problem.
One that readily comes to mind (although it may not be ideal) is to require also that the sum
sum((x(2:23)-x(1:22)).^2)
be a minimum. In that form the problem can be solved in matlab using the Lagrange undetermined multiplier method as follows:
A = diag(-ones(20,1),1)+diag(2*ones(21,1))+diag(-ones(20,1),-1);
A = [0,diff(C');diff(C),A];
b = [y1-y23-C(1)*x1+C(22)*x23;x1;zeros(19,1);x23];
X = A\b; % The first element of X will be the Lagrange multiplier
x = [x1;X(2:22);x23];
y = cumsum([y1;C.*diff(x)]);
This gives a reasonable answer with your given C array provided you have a slope value for (y23-y1)/(x23-x1) lying somewhere between 1.0 and 1.4. With values outside this range minimization of the above sum unfortunately yields a non-monotone x array, so you may prefer to adopt some other more esoteric minimization criterion. If you have the Optimization Toolbox, you can use 'fmincon' to minimize whatever function you choose, subject to the constraint of the one equation.

Categories

Find more on Variables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!