0

I have a vector N which has 21 elements

N=[3900000 5300000 7200000 9600000 12900000 17100000 23100000 ...
    31400000 38600000 50200000 62900000 76000000 92000000 ...
    105700000 122800000 131700000 150700000 179000000 205000000 ...
    226500000 248700000];

I first need to construct another vector Nprin=(N_{i+1}-N_{i-1})/20 so this is how I construct it

Nprin=(N(3:end)-N(1:end-2))/20;

I now need to divide N by Nprin elementwise (let's call it y) but N has 21 elements while Nprin has 19 elements so is this how I should evaluate y?

y=Nprin./N(1:end-2);

or is there a way that allows me simply do the following?

y=Nprin./N
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • 3
    No the element wise operators will only work for vectors or matrices of the same size (_except with a scalar value_). It will not automatically stop at the end of the shortest vector. – Hoki Aug 09 '16 at 11:03
  • You should first decide what you want to do when you have no more elements in the shortest vector? Negate them? divide by 1? – EBH Aug 09 '16 at 11:12
  • What is your desired output? – Sardar Usama Aug 09 '16 at 11:13
  • 1
    The question here is beyond the technical aspect of dividing two vectors, as you have already shown that you know how to do it. The problem is with computing derivatives, which are not known outside your domain. You can either choose to set them to some value or to extrapolate them, but I personally think that you should just throw out the first and last values of `N`... The "right" solution would depend on the specific needs/constraints of your problem. – Dev-iL Aug 09 '16 at 11:35
  • In your specific case adding a value at the beggining and the end of the shortest vector will align the two vectors so the element `i` is aligned with the derivative at this point. For the head and the tail you can fill with zeros, the closest value (or some higher degree of extrapolation), or just chop your longer vector. If you want to add zeros just do `y = [0 Nprin 0] ./ N` –  Aug 09 '16 at 11:42
  • Are we discussing how to evaluate the boundaries of a differential equation it is much more complicated than this. Assuming you are using a method like the trapetzoidal method, how would you find the boundary of your second point so that the accuracy can be O(n^2)? There are courses on university where whole lectures covers this problem. And this is for 1 dimensional problems. For the 2 dimensional problem you can count on about half part of a 200 hours course. I suggest that you try to set up a strategy how to solve this problem. – patrik Aug 09 '16 at 12:14
  • @patrik: Here is my actual problem: http://math.stackexchange.com/questions/1887022 – Justine Lawless Aug 09 '16 at 13:04
  • @JustineLawless I am no differential equations expert and have not been looking at one for years. I just recalled something I heard long ago and felt that it sounded like your problem. I hinted this so that you would know where to start looking. Anyway, you need to solve what is called a "boundary value problem". How this is done I believe I best leave to the maths experts on maths SE. Also keep googling on how to solve boundary value problems for your method (trapezoidal rule?) and add your boundary conditions to the maths SE post. – patrik Aug 09 '16 at 15:09

0 Answers0