Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6761153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:09:07+00:00 2026-05-26T14:09:07+00:00

To all: This question pertains to some MS research I am doing. What I

  • 0

To all:

This question pertains to some MS research I am doing.

What I want to do is seemingly simple. I have a text file of values( time, …..values). I then want to integrate from 0 to those values for all times, then save that value to a text file.

from numpy import *
from pylab import *
import os, sys, shutil
import math

#######################

#Load Data
data = loadtxt('wh.txt')

#Open file to save plots to
shutil.rmtree("wh_files")
os.makedirs("wh_files")
os.chdir("wh_files")

for i in range(0,100,1):
   int = trapz(data[i,:],axis=0)
   print int
   savetxt('int.txt', int)

When I run this I get the following error:

  File "integral.py", line 19, in 
    savetxt('int.txt', int)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/numpy/lib/npyio.py", line 960, in savetxt
    ncol = X.shape[1]
IndexError: tuple index out of range

I have been trying to solve this problem for a few days on an off now, but am yet to find a solution. I appreciate any help/comments you can give.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:09:08+00:00Added an answer on May 26, 2026 at 2:09 pm

    The value of int is a float but savetxt requires an array. You want to create a numpy array for all the integration results, then save it at the end using savetxt. I think something like this should do the trick:

    int_array = apply_along_axis(trapz, 0, data)
    savetxt('int.txt', int_array)
    

    Keep in mind that this (along with your original example) will sum the time fields, rather than integrating across time. This will only yield valid results if the spacing along the x-axis is 1. Otherwise you’ll want to do something like this:

    t = data[0,:]
    int_array = apply_along_axis(trapz, 0, data[1:,:], t)
    savetxt('int.txt', int_array)
    

    Assuming the time fields are numeric.

    edit: Further explanation of 2nd code section follows.

    You’re using the trapezoidal rule to integrate a variety of values, which is an integration approximation technique that works by summing the average of successive y-values on a curve multiplied by the change in x between the two y-values. This amounts to calculating the area of a trapezoid that connects the two y-values and the x-axis, like so:

    Trapezoid Rule

    It’s not completely clear from your question, but it seemed that you were integrating the values across time, so that the x-axis would represent time. The x-values need to be incorporated to get the correct area of each trapezoid (the area of each trapezoid is (x2 – x1) * (y2 + y1) / 2 and the final integration result is the sum of all such areas).

    The simplest way to incorporate these x-axis values is to pass it into the trapz function as the x parameter (see the docstring). In the example above, I used t = data[0,:] as the array of x values suchly.

    Another caveat: if all spacing between x values are the same (so that x2 – x1 is a constant), you can save yourself some calculation by pulling this out of the summation and simply multiplying it at the end. This functionality is available through the trapz function with the dx parameter. So, if you’re time measurements were taken every 30 seconds, for example, you could replace the 2nd line in my 2nd example with:

    int_array = apply_along_axis(trapz, 0, data[1:,:], None, 30)
    

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Dear all,Now i have this question in my java program,I think it should be
This might be a stupid question but I'm new at all this. I want
Afternoon all, My question pertains to 2 chained forms, where values from the first
all This question is related to two methods in System.IO.File: File.WriteAllLines and File.ReadAllLines When
This question pertains to pretty much all C-like curly bracket programming languages. I'm talking
First of all: This question is not directly programming related. However, the problem only
Well, first of all sorry about this question it must be pretty straight forward
This question Check all associations before destroy in rails suggests using :dependent => :restrict
All day I was looking an answer on this question: How to log in
Im sure this question is so easy for all you experts, but I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.