Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm new to Mathematica and cannot figure out a way to plot a 2d hermite spline. My points are: $p_0=\{143.05;195.606\};$ $p_1=\{159.373;181.845\};$ $p_2=\{160.891;179.26\};$ $p_3=\{176.423;171.878\};$

I need to plot the spline from $p_0$ to $p_1$

I calculate Catmull-Rom tangents in the following way: $\left(t_1=0.5 \left(p_2-p_0\right);\right)$ $\left(t_2=0.5 \left(p_3-p_1\right);\right)$

Calculating the weights: $$ \left(h_1(\text{t$\_$})\text{:=}2 t^3-3 t^2+1;\right) \left(h_2(\text{t$\_$})\text{:=}3 t^2-2 t^3;\right) \left(h_3(\text{t$\_$})\text{:=}t^3-2 t^2+t;\right) \left(h_4(\text{t$\_$})\text{:=}t^3-t^2;\right) $$

And finaly the spline function: $$ P(\text{t$\_$})\text{:=}\left\{h_1 p_1.x+h_2 p_2.x+h_3 t_1.x+4 h_4 t_2.x;h_1 p_1.y+h_2 p_2.y+h_3 t_1.y+4 h_4 t_2.y\right\}; $$

And trying to plot it: $$\text{ParametricPlot}[\{P(t)\},\{t,0,1\}]$$

But I'm getting some strange errors:

SetDelayed::write: Tag Plus in (1-3 t^2+2 t^3)[t_] is Protected. >>

What does it mean? Please provide me with a complete solution to plotting a hermite spline. Thanks!

UPDATE: My mathematica code is:

Subscript[p, 0] = {143.0497; 195.6056};
Subscript[p, 1] = {159.3734; 181.8449};
Subscript[p, 2] = {160.8912; 179.2604};
Subscript[p, 3] = {176.423; 171.8781};

Subscript[t, 1] = (Subscript[p, 2] - Subscript[p, 0]) * 0.5;
Subscript[t, 2] = (Subscript[p, 3] - Subscript[p, 1]) * 0.5;

Subscript[h, 1][t_] := 2 * t^3 - 3*t^2 + 1;
Subscript[h, 2][t_] := -2*t^3 + 3*t^2;
Subscript[h, 3][t_] := t^3 - 2*t^2 + t;
Subscript[h, 4][t_] := t^3 - t^2;

P[t_] := {Subscript[h, 1][t] * Subscript[p, 1].x + 
    Subscript[h, 2][t] * Subscript[p, 2].x + 
    Subscript[h, 3][t] * Subscript[t, 1].x + 
    Subscript[h, 4] [t]* Subscript[t, 2].x; 
   Subscript[h, 1][t] * Subscript[p, 1].y + 
    Subscript[h, 2][t] * Subscript[p, 2].y + 
    Subscript[h, 3][t] * Subscript[t, 1].y + 
    Subscript[h, 4][t] * Subscript[t, 2].y};

ParametricPlot[{P[t]}, {t, 0, 1}]
share|improve this question

closed as off-topic by Sjoerd C. de Vries, Artes, m_goldberg, rm -rf Oct 26 '13 at 14:27

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Sjoerd C. de Vries, Artes, m_goldberg, rm -rf
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Hello Ilya:) Please post the actual Mathematica code instead of LateX form. –  Kuba Oct 26 '13 at 8:34
    
@Kuba please take a look at the code I posted. Thanks a lot! –  Ilya Suzdalnitski Oct 26 '13 at 8:55
    
What is x inside the definition of P[t]? Also you probably need to put there Subscript[h, 1][t] etc. instead of Subscript[h, 1] alone. –  Kuba Oct 26 '13 at 9:00
4  
Since you're new to Mathematica, here's a piece of advise, refrain from using subscripts. –  RunnyKine Oct 26 '13 at 9:06
2  
The Dot or . is the operator for the inner product of vectors. It is not an operator to obtain elements from a struct or an object, like it is in some other languages. If you want to get pieces of lists there are dedicated functions for that. –  Sjoerd C. de Vries Oct 26 '13 at 11:20

1 Answer 1

up vote 2 down vote accepted

As already mentioned in the comments, you shouldn't be using subscripts. You can find more about that here.

There are a number of additional issues. Using ; as list separator, misuse of the . operator, and not using Mathematica's vector capabilities.

A direct conversion of your code would be:

p0 = {143.0497, 195.6056};
p1 = {159.3734, 181.8449};
p2 = {160.8912, 179.2604};
p3 = {176.423, 171.8781};

t1 = (p2 - p0)*0.5;
t2 = (p3 - p1)*0.5;

h1[t_] := 2*t^3 - 3*t^2 + 1;
h2[t_] := -2*t^3 + 3*t^2;
h3[t_] := t^3 - 2*t^2 + t;
h4[t_] := t^3 - t^2;

P[t_] := {h1[t] p1[[1]] + h2[t] p2[[1]] + h3[t] t1[[1]] + h4[t] t2[[1]],
   h1[t] p1[[2]] + h2[t] p2[[2]] + h3[t]*t1[[2]] + h4[t] t2[[2]]};

ParametricPlot[P[t], {t, 0, 1}]

Using vectors instead of vector components, P could be written much shorter and clearer:

P[t_] := h1[t] p1 + h2[t] p2 + h3[t] t1 + h4[t] t2;
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.