# Plot function with changing variable

2 views (last 30 days)
Eric on 24 May 2015
Commented: Eric on 24 May 2015
So I have a function that approximately calculates the length of the space curve 2*sin(s),3*cos(s)*exp(s),s.^2/10.
function leng_straight = spacecurvelength(curve,a,b,n)
% Generating n x-points from a to b
syms s
xi= a:(b-a)/n:b;
yi=subs(curve,s,xi);% generating the y-values of the function
% assuming that between consecutive data points, the
% curve can be approximated by linear splines.
leng_straight=0;
m=length(xi);
% there are m-1 splines for m points
for i=1:1:m-1
dx=xi(i+1)-xi(i);
dy= yi(i+1)-yi(i);
leneach=sqrt(dx^2+dy^2);
leng_straight=leng_straight+leneach;
end
end
So I'm now trying to plot this function as I change the 'n' value which is the number of subintervals. Right now I'm getting a blank plot when I call it like this: I've tried changing the names of each of the 'length's to make it unique but no difference.
length = spacecurvelength(@(s) [2*sin(s),3*cos(s)*exp(s),s.^2/10],-4*pi,4*pi,2);
length = spacecurvelength(@(s) [2*sin(s),3*cos(s)*exp(s),s.^2/10],-4*pi,4*pi,5);
length = spacecurvelength(@(s) [2*sin(s),3*cos(s)*exp(s),s.^2/10],-4*pi,4*pi,10);
length = spacecurvelength(@(s) [2*sin(s),3*cos(s)*exp(s),s.^2/10],-4*pi,4*pi,20);
x=[2, 5, 10, 20];
y=length;
plot(x,y)
xlabel('n')
ylabel('length')
title(['Length of Space Curve as a function of n'])

Walter Roberson on 24 May 2015
curvelen = arrayfun(@(n) spacecurvelength(@(s) [2*sin(s), 3*cos(s)*exp(s), s.^2/10],-4*pi,4*pi,n), x);
plot(x, curvelen);
Note that using a variable named "length" is likely to cause trouble with using the MATLAB library function named "length".
Eric on 24 May 2015
Yes that works. Thanks Walter. Appreciate it.