You are Here: Home >< Maths

# MATLAB help - Approximating areas below curves

Announcements Posted on
Last day to win £100 of Amazon vouchers - don't miss out! Take our quick survey to enter 24-10-2016
1. The below code is for Q1, does anybody know how I could possible edit it so that it could apply to Q2?

" clear all;
format long;
n = 80;
a = 0; b = 1;
h = (b-a)/n;
x = linspace(a,b,(n+1));
integral = h.*sum(exp(x(1:n))) "

This is for e^x between 0 and 1
2. Hi,

If you look at from your code from Q1 in MATLAB (in your console simply enter x(1:n) after running your code) you will see that it it stores values for steps 1-80, however your linspace function is a 1x81 double.
This is a clue how to solve Q2. You need shift your initial starting and final steps forward one step higher (as denoted by ) so it runs from 2-81.
Spoiler:
Show

If you get it working and want to check if you're getting the correct value, you can add the following and if your value for Q3 is the same to 4 S.F. you're dandy.

fun = @(x) exp(x); % Setup Function for Integration

q = integral(fun,0,1) % Integrate and print function to console

## Register

Thanks for posting! You just need to create an account in order to submit the post
1. this can't be left blank
2. this can't be left blank
3. this can't be left blank

6 characters or longer with both numbers and letters is safer

4. this can't be left empty
1. Oops, you need to agree to our Ts&Cs to register

Updated: October 14, 2016
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Today on TSR

### Who is getting a uni offer this half term?

Find out which unis are hot off the mark here

Poll
Useful resources

### Maths Forum posting guidelines

Not sure where to post? Read here first

### How to use LaTex

Writing equations the easy way

### Study habits of A* students

Top tips from students who have already aced their exams