Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
problem at quad

Subject: problem at quad

From: bo

Date: 12 Apr, 2010 16:31:06

Message: 1 of 10

Hi ,

I wrote the below program and tried to find the arc length between 19.8831,15.4276 these two points

y=[235.69,267.97,291.67,303.98,305.59,302.03,285.44,257.99,224.26,190.37,160.33;
z=[85.24,73.95,49.29,17.39,-16.77,-50.79,-80.7,-101.09,-106.73,-102.09,-85.75];
r = sqrt(y.^2+z.^2);
degree=(asin(z./r)*180/pi);
p_rd_2nd=polyfit(degree,r,2);
disp(p_rd_2nd);
degree_vs_r_2nd=-30:2:20;
radius=170:10:260;
rd_2nd=polyval(p_rd_2nd,degree_vs_r_2nd);
plot(degree,r,'o',degree_vs_r_2nd,rd_2nd); hold;
len=quad(@arclength,19.8831,15.4276)
--------------------------------------------
The function file:
function f=arclength_2nd(a)
f=-0.1481*(a^2)-0.5033*(a)+316.2390;

But I encoutered In quad at 64
??? Attempt to execute SCRIPT arclength as a function.

Error in ==> quad at 64
y = feval(f,x,varargin{:}); y = y(:).';

How could change the script to a function ? I am quite new to matlab.Thanks in advance.

Subject: problem at quad

From: Steven Lord

Date: 13 Apr, 2010 02:29:10

Message: 2 of 10


"bo " <bobpong1979@hotmail.com> wrote in message
news:hpvhsa$1hp$1@fred.mathworks.com...
> Hi ,
>
> I wrote the below program and tried to find the arc length between
> 19.8831,15.4276 these two points
>
> y=[235.69,267.97,291.67,303.98,305.59,302.03,285.44,257.99,224.26,190.37,160.33;
> z=[85.24,73.95,49.29,17.39,-16.77,-50.79,-80.7,-101.09,-106.73,-102.09,-85.75];
> r = sqrt(y.^2+z.^2);
> degree=(asin(z./r)*180/pi);
> p_rd_2nd=polyfit(degree,r,2);
> disp(p_rd_2nd);
> degree_vs_r_2nd=-30:2:20;
> radius=170:10:260;
> rd_2nd=polyval(p_rd_2nd,degree_vs_r_2nd);
> plot(degree,r,'o',degree_vs_r_2nd,rd_2nd); hold;
> len=quad(@arclength,19.8831,15.4276)
> --------------------------------------------
> The function file:
> function f=arclength_2nd(a)
> f=-0.1481*(a^2)-0.5033*(a)+316.2390;
>
> But I encoutered In quad at 64
> ??? Attempt to execute SCRIPT arclength as a function.

Did you put the first block of code in a script file named arclength.m and
the function arclength_2nd in a function arclength_2nd.m? If so, you need
to call QUAD with @arclength_2nd as the first input, not @arclength.

You should also replace the (a^2) with (a.^2) in arclength_2nd.m.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: problem at quad

From: John D'Errico

Date: 13 Apr, 2010 03:20:06

Message: 3 of 10

"bo " <bobpong1979@hotmail.com> wrote in message <hpvhsa$1hp$1@fred.mathworks.com>...
> Hi ,
>
> I wrote the below program and tried to find the arc length between 19.8831,15.4276 these two points
>
> y=[235.69,267.97,291.67,303.98,305.59,302.03,285.44,257.99,224.26,190.37,160.33;
> z=[85.24,73.95,49.29,17.39,-16.77,-50.79,-80.7,-101.09,-106.73,-102.09,-85.75];
> r = sqrt(y.^2+z.^2);
> degree=(asin(z./r)*180/pi);
> p_rd_2nd=polyfit(degree,r,2);
> disp(p_rd_2nd);
> degree_vs_r_2nd=-30:2:20;
> radius=170:10:260;
> rd_2nd=polyval(p_rd_2nd,degree_vs_r_2nd);
> plot(degree,r,'o',degree_vs_r_2nd,rd_2nd); hold;
> len=quad(@arclength,19.8831,15.4276)
> --------------------------------------------
> The function file:
> function f=arclength_2nd(a)
> f=-0.1481*(a^2)-0.5033*(a)+316.2390;
>
> But I encoutered In quad at 64
> ??? Attempt to execute SCRIPT arclength as a function.
>
> Error in ==> quad at 64
> y = feval(f,x,varargin{:}); y = y(:).';
>
> How could change the script to a function ? I am quite new to matlab.Thanks in advance.

There are other problems here beyond the mechanical
on that your function failed on. You cannot quite so
trivially convert to polar coordinates.

Where is the origin in this polar coordinate system?
Don't forget that when you do as you did, you
implicitly make an assumption about the location of
that polar origin. When you make this incorrect
assumption about the origin, that quadratic polynomial
now becomes wildly insufficient as a model for the
curve that you try to fit it with. If you don't believe
me, PLOT IT!!!!!!!!

plot(degree,r)

Do you think that this curve looks like a quadratic
polynomial as you have generated it????????

Think about what you are doing, BEFORE you blindly
do things. ALWAYS PLOT YOUR DATA.

I'll point out that I did submit a function to compute
the arclength of a general curve to the FEX, although
as this is homework, you will not find it to be of value.

John

Subject: problem at quad

From: bo

Date: 13 Apr, 2010 09:25:08

Message: 4 of 10

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <hq0nt6$m9a$1@fred.mathworks.com>...
> "bo " <bobpong1979@hotmail.com> wrote in message <hpvhsa$1hp$1@fred.mathworks.com>...
> > Hi ,
> >
> > I wrote the below program and tried to find the arc length between 19.8831,15.4276 these two points
> >
> > y=[235.69,267.97,291.67,303.98,305.59,302.03,285.44,257.99,224.26,190.37,160.33;
> > z=[85.24,73.95,49.29,17.39,-16.77,-50.79,-80.7,-101.09,-106.73,-102.09,-85.75];
> > r = sqrt(y.^2+z.^2);
> > degree=(asin(z./r)*180/pi);
> > p_rd_2nd=polyfit(degree,r,2);
> > disp(p_rd_2nd);
> > degree_vs_r_2nd=-30:2:20;
> > radius=170:10:260;
> > rd_2nd=polyval(p_rd_2nd,degree_vs_r_2nd);
> > plot(degree,r,'o',degree_vs_r_2nd,rd_2nd); hold;
> > len=quad(@arclength,19.8831,15.4276)
> > --------------------------------------------
> > The function file:
> > function f=arclength_2nd(a)
> > f=-0.1481*(a^2)-0.5033*(a)+316.2390;
> >
> > But I encoutered In quad at 64
> > ??? Attempt to execute SCRIPT arclength as a function.
> >
> > Error in ==> quad at 64
> > y = feval(f,x,varargin{:}); y = y(:).';
> >
> > How could change the script to a function ? I am quite new to matlab.Thanks in advance.
>
> There are other problems here beyond the mechanical
> on that your function failed on. You cannot quite so
> trivially convert to polar coordinates.
>
> Where is the origin in this polar coordinate system?
> Don't forget that when you do as you did, you
> implicitly make an assumption about the location of
> that polar origin. When you make this incorrect
> assumption about the origin, that quadratic polynomial
> now becomes wildly insufficient as a model for the
> curve that you try to fit it with. If you don't believe
> me, PLOT IT!!!!!!!!
>
> plot(degree,r)
>
> Do you think that this curve looks like a quadratic
> polynomial as you have generated it????????
>
> Think about what you are doing, BEFORE you blindly
> do things. ALWAYS PLOT YOUR DATA.
>
> I'll point out that I did submit a function to compute
> the arclength of a general curve to the FEX, although
> as this is homework, you will not find it to be of value.
>
> John

Hi,John

It is like a 4th degree polynomial.I used quadratic function because I want to find the arc length between each pair of data.The origin of the cordinate is 0,0 but my radius data ranges only from 180 to 310

Subject: problem at quad

From: bo

Date: 13 Apr, 2010 09:27:05

Message: 5 of 10

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <hq0nt6$m9a$1@fred.mathworks.com>...
> "bo " <bobpong1979@hotmail.com> wrote in message <hpvhsa$1hp$1@fred.mathworks.com>...
> > Hi ,
> >
> > I wrote the below program and tried to find the arc length between 19.8831,15.4276 these two points
> >
> > y=[235.69,267.97,291.67,303.98,305.59,302.03,285.44,257.99,224.26,190.37,160.33;
> > z=[85.24,73.95,49.29,17.39,-16.77,-50.79,-80.7,-101.09,-106.73,-102.09,-85.75];
> > r = sqrt(y.^2+z.^2);
> > degree=(asin(z./r)*180/pi);
> > p_rd_2nd=polyfit(degree,r,2);
> > disp(p_rd_2nd);
> > degree_vs_r_2nd=-30:2:20;
> > radius=170:10:260;
> > rd_2nd=polyval(p_rd_2nd,degree_vs_r_2nd);
> > plot(degree,r,'o',degree_vs_r_2nd,rd_2nd); hold;
> > len=quad(@arclength,19.8831,15.4276)
> > --------------------------------------------
> > The function file:
> > function f=arclength_2nd(a)
> > f=-0.1481*(a^2)-0.5033*(a)+316.2390;
> >
> > But I encoutered In quad at 64
> > ??? Attempt to execute SCRIPT arclength as a function.
> >
> > Error in ==> quad at 64
> > y = feval(f,x,varargin{:}); y = y(:).';
> >
> > How could change the script to a function ? I am quite new to matlab.Thanks in advance.
>
> There are other problems here beyond the mechanical
> on that your function failed on. You cannot quite so
> trivially convert to polar coordinates.
>
> Where is the origin in this polar coordinate system?
> Don't forget that when you do as you did, you
> implicitly make an assumption about the location of
> that polar origin. When you make this incorrect
> assumption about the origin, that quadratic polynomial
> now becomes wildly insufficient as a model for the
> curve that you try to fit it with. If you don't believe
> me, PLOT IT!!!!!!!!
>
> plot(degree,r)
>
> Do you think that this curve looks like a quadratic
> polynomial as you have generated it????????
>
> Think about what you are doing, BEFORE you blindly
> do things. ALWAYS PLOT YOUR DATA.
>
> I'll point out that I did submit a function to compute
> the arclength of a general curve to the FEX, although
> as this is homework, you will not find it to be of value.
>
> John

If it is 4th degree polynomial, could I use inty () to find its arc length between 2 points

Subject: problem at quad

From: John D'Errico

Date: 13 Apr, 2010 11:01:08

Message: 6 of 10

"bo " <bobpong1979@hotmail.com> wrote in message <hq1d9k$17c$1@fred.mathworks.com>...

> > There are other problems here beyond the mechanical
> > on that your function failed on. You cannot quite so
> > trivially convert to polar coordinates.
> >
> > Where is the origin in this polar coordinate system?
> > Don't forget that when you do as you did, you
> > implicitly make an assumption about the location of
> > that polar origin. When you make this incorrect
> > assumption about the origin, that quadratic polynomial
> > now becomes wildly insufficient as a model for the
> > curve that you try to fit it with. If you don't believe
> > me, PLOT IT!!!!!!!!
> >
> > plot(degree,r)
> >
> > Do you think that this curve looks like a quadratic
> > polynomial as you have generated it????????
> >
> > Think about what you are doing, BEFORE you blindly
> > do things. ALWAYS PLOT YOUR DATA.
> >
> > I'll point out that I did submit a function to compute
> > the arclength of a general curve to the FEX, although
> > as this is homework, you will not find it to be of value.
> >
> > John
>
> Hi,John
>
> It is like a 4th degree polynomial.

NO. It is not.

LOOK AT THE DATA. PLOT IT. THINK ABOUT IT.
I plotted your curve.

plot(degree,R)

As a function of the independent variable degree,
this is not even a single valued function as you have
computed it, not with that implicit origin.


> I used quadratic function because I want to find the arc length between each pair of data.The origin of the cordinate is 0,0 but my radius data ranges only from 180 to 310

Sigh. A quadratic function is no better.

Try plotting the function as you have fit it, compared
to the actual data points. I don't care what order
polynomial you use. Polynomials simply are not
multi-valued functions. They do not have points where
they go through derivative singularities. Or perhaps you
just use better polynomials than I do. No.

If this is not homework, then you can download
the function I put on the FEX. I compute arclength
in a way there that is fully independent of the
orientation of the curve. It may be any curve in
any number of dimensions. I allow you to compute
a linear chordal arclength or that which you get
from a parametric spline or pchip model.

The arclength function that I posted also returns the
length of each segment of the curve.

http://www.mathworks.com/matlabcentral/fileexchange/26848-arclength

y=[235.69,267.97,291.67,303.98,305.59,302.03,285.44,257.99,224.26,190.37,160.33];
z=[85.24,73.95,49.29,17.39,-16.77,-50.79,-80.7,-101.09,-106.73,-102.09,-85.75];

[totallenth,seglength] = arclength(y,z,'linear')
totallenth =
       341.99
seglength =
       34.197
       34.202
       34.193
       34.198
       34.206
       34.203
       34.194
       34.198
       34.206
       34.196

[totallength,seglength] = arclength(y,z,'spline')
totallenth =
       344.18
seglength =
       34.585
       34.456
       34.402
       34.256
       34.305
       34.478
       34.502
       34.417
        34.33
       34.452

As you would expect, the spline fit is longer than the
linear one. This must be, since a connect-the-dots
curve is the shortest possible way to connect the
points in that sequence.

If you insist on writing your own code for this, then
do it well. See what happens when you use a different
origin. (Sigh. Why do I need to go to this extent?)

y0 = mean(y);
z0 = mean(z);
r = sqrt((y-y0).^2+(z-z0).^2);
degree = atan2(z-z0,y-y0)*180/pi;
plot(degree,r)

Try it. It should be no surprise that the is a viable
functional form. Of course, the arclength is incorrect
when you do it that way, since the variables of the
curve now have the units of degrees on one axis,
and radius on the other.

So regardless of how you try to do it using degrees,
the arclength of that curve will be meaningless.

But feel free to do meaningless computations. They
don't charge you more for your license if you do.
I would strongly recommend that you don't publish
what you compute this way though. (Sorry, but the
sarcasm is coming through here.)

John

Subject: problem at quad

From: Mark Shore

Date: 13 Apr, 2010 12:23:21

Message: 7 of 10

"bo " <bobpong1979@hotmail.com> wrote in message <hq1dd9$2eh$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <hq0nt6$m9a$1@fred.mathworks.com>...
> > "bo " <bobpong1979@hotmail.com> wrote in message <hpvhsa$1hp$1@fred.mathworks.com>...
> > > Hi ,
> > >
> > > I wrote the below program and tried to find the arc length between 19.8831,15.4276 these two points
> > >
> > > y=[235.69,267.97,291.67,303.98,305.59,302.03,285.44,257.99,224.26,190.37,160.33;
> > > z=[85.24,73.95,49.29,17.39,-16.77,-50.79,-80.7,-101.09,-106.73,-102.09,-85.75];
> > > r = sqrt(y.^2+z.^2);
> > > degree=(asin(z./r)*180/pi);
> > > p_rd_2nd=polyfit(degree,r,2);
> > > disp(p_rd_2nd);
> > > degree_vs_r_2nd=-30:2:20;
> > > radius=170:10:260;
> > > rd_2nd=polyval(p_rd_2nd,degree_vs_r_2nd);
> > > plot(degree,r,'o',degree_vs_r_2nd,rd_2nd); hold;
> > > len=quad(@arclength,19.8831,15.4276)
> > > --------------------------------------------
> > > The function file:
> > > function f=arclength_2nd(a)
> > > f=-0.1481*(a^2)-0.5033*(a)+316.2390;
> > >
> > > But I encoutered In quad at 64
> > > ??? Attempt to execute SCRIPT arclength as a function.
> > >
> > > Error in ==> quad at 64
> > > y = feval(f,x,varargin{:}); y = y(:).';
> > >
> > > How could change the script to a function ? I am quite new to matlab.Thanks in advance.
> >
> > There are other problems here beyond the mechanical
> > on that your function failed on. You cannot quite so
> > trivially convert to polar coordinates.
> >
> > Where is the origin in this polar coordinate system?
> > Don't forget that when you do as you did, you
> > implicitly make an assumption about the location of
> > that polar origin. When you make this incorrect
> > assumption about the origin, that quadratic polynomial
> > now becomes wildly insufficient as a model for the
> > curve that you try to fit it with. If you don't believe
> > me, PLOT IT!!!!!!!!
> >
> > plot(degree,r)
> >
> > Do you think that this curve looks like a quadratic
> > polynomial as you have generated it????????
> >
> > Think about what you are doing, BEFORE you blindly
> > do things. ALWAYS PLOT YOUR DATA.
> >
> > I'll point out that I did submit a function to compute
> > the arclength of a general curve to the FEX, although
> > as this is homework, you will not find it to be of value.
> >
> > John
>
> If it is 4th degree polynomial, could I use inty () to find its arc length between 2 points


If anything, your data points appear to fit a rotated ellipse centered near (230,-10), so your choice of (0,0) as the origin for the coordinate transformation is inappropriate. Without further information as to the actual generating function, there is no unique arc length between points - one could certainly derive many parametric equations that fit those 11 points yet do not form a closed convex curve. Or even a high-order polynomial - why not?

Subject: problem at quad

From: bo

Date: 14 Apr, 2010 00:43:04

Message: 8 of 10

"Mark Shore" <mshore@magmageosciences.ca> wrote in message <hq1nno$7t2$1@fred.mathworks.com>...
> "bo " <bobpong1979@hotmail.com> wrote in message <hq1dd9$2eh$1@fred.mathworks.com>...
> > "John D'Errico" <woodchips@rochester.rr.com> wrote in message <hq0nt6$m9a$1@fred.mathworks.com>...
> > > "bo " <bobpong1979@hotmail.com> wrote in message <hpvhsa$1hp$1@fred.mathworks.com>...
> > > > Hi ,
> > > >
> > > > I wrote the below program and tried to find the arc length between 19.8831,15.4276 these two points
> > > >
> > > > y=[235.69,267.97,291.67,303.98,305.59,302.03,285.44,257.99,224.26,190.37,160.33;
> > > > z=[85.24,73.95,49.29,17.39,-16.77,-50.79,-80.7,-101.09,-106.73,-102.09,-85.75];
> > > > r = sqrt(y.^2+z.^2);
> > > > degree=(asin(z./r)*180/pi);
> > > > p_rd_2nd=polyfit(degree,r,2);
> > > > disp(p_rd_2nd);
> > > > degree_vs_r_2nd=-30:2:20;
> > > > radius=170:10:260;
> > > > rd_2nd=polyval(p_rd_2nd,degree_vs_r_2nd);
> > > > plot(degree,r,'o',degree_vs_r_2nd,rd_2nd); hold;
> > > > len=quad(@arclength,19.8831,15.4276)
> > > > --------------------------------------------
> > > > The function file:
> > > > function f=arclength_2nd(a)
> > > > f=-0.1481*(a^2)-0.5033*(a)+316.2390;
> > > >
> > > > But I encoutered In quad at 64
> > > > ??? Attempt to execute SCRIPT arclength as a function.
> > > >
> > > > Error in ==> quad at 64
> > > > y = feval(f,x,varargin{:}); y = y(:).';
> > > >
> > > > How could change the script to a function ? I am quite new to matlab.Thanks in advance.
> > >
> > > There are other problems here beyond the mechanical
> > > on that your function failed on. You cannot quite so
> > > trivially convert to polar coordinates.
> > >
> > > Where is the origin in this polar coordinate system?
> > > Don't forget that when you do as you did, you
> > > implicitly make an assumption about the location of
> > > that polar origin. When you make this incorrect
> > > assumption about the origin, that quadratic polynomial
> > > now becomes wildly insufficient as a model for the
> > > curve that you try to fit it with. If you don't believe
> > > me, PLOT IT!!!!!!!!
> > >
> > > plot(degree,r)
> > >
> > > Do you think that this curve looks like a quadratic
> > > polynomial as you have generated it????????
> > >
> > > Think about what you are doing, BEFORE you blindly
> > > do things. ALWAYS PLOT YOUR DATA.
> > >
> > > I'll point out that I did submit a function to compute
> > > the arclength of a general curve to the FEX, although
> > > as this is homework, you will not find it to be of value.
> > >
> > > John
> >
> > If it is 4th degree polynomial, could I use inty () to find its arc length between 2 points
>
>
> If anything, your data points appear to fit a rotated ellipse centered near (230,-10), so your choice of (0,0) as the origin for the coordinate transformation is inappropriate. Without further information as to the actual generating function, there is no unique arc length between points - one could certainly derive many parametric equations that fit those 11 points yet do not form a closed convex curve. Or even a high-order polynomial - why not?


Ok.Mark, Thanks a lot!

Subject: problem at quad

From: Mark Shore

Date: 14 Apr, 2010 01:46:04

Message: 9 of 10

> > If anything, your data points appear to fit a rotated ellipse centered near (230,-10), so your choice of (0,0) as the origin for the coordinate transformation is inappropriate. Without further information as to the actual generating function, there is no unique arc length between points - one could certainly derive many parametric equations that fit those 11 points yet do not form a closed convex curve. Or even a high-order polynomial - why not?
>
>
> Ok.Mark, Thanks a lot!

I hope you're referring to the ellipse suggestion...

Subject: problem at quad

From: bo

Date: 14 Apr, 2010 02:01:05

Message: 10 of 10

"Mark Shore" <mshore@magmageosciences.ca> wrote in message <hq36os$seg$1@fred.mathworks.com>...
> > > If anything, your data points appear to fit a rotated ellipse centered near (230,-10), so your choice of (0,0) as the origin for the coordinate transformation is inappropriate. Without further information as to the actual generating function, there is no unique arc length between points - one could certainly derive many parametric equations that fit those 11 points yet do not form a closed convex curve. Or even a high-order polynomial - why not?
> >
> >
> > Ok.Mark, Thanks a lot!
>
> I hope you're referring to the ellipse suggestion...

Yes.I will look into it.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us