You are Here: Home >< Maths

# Stuck on MatLab Code watch

1. It's a question on MatLab Integration program, but have a look at this code first. It's a part of the Integration on Rectangle Method program:

[LINES 1-3]
x = 1 : 2 : 5; % x=lower limit: strip width: upper limit
% These are not supposed to be constants, but for this question's simplicity, let's keep it that way.

[LINE 4]
y_x = input ( 'Enter your function of x = ' )
% allowing the user to modify the function as required

[LINE 5]
S = 2 .* ( sum (abs (y_x)) )

Now, that works fine with the subsequent lines to calculate the approximation of the integration; but, I want what was input by the user for y_x to be also used for the plot TITLE, DISP, and others. The issue is y_x turns to an array instantly. If I turn the input into string [Ex: y_x = input ( ' Enter you function of x = ' , 's') ], it works with the DISP and TITLE, but it doesn't process the x array, so I don't get the integration approximation. Example, say x.^2 was input. Hence, y_x = x.^2

[LINE 6 - 7]
plot( x , y_x)
title(['Numeric Integration for' , num2str(y_x) , 'with 2 slices')

Instead of title being 'Numeric Integration for x.^2 with 2 slices', what shows is 'Numeric Integration for 1 9 25 with 2 slices.' [1 9 25] is the squares for x=1 : 2 : 5. That's because the input has turned to an array and info is lost. Anyone knows how to get around this problem?
2. mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm chemeng
3. LOL no wonder I thought the reply was pretty quick
4. I did a bit of googling and found this: http://www.mathworks.co.uk/help/matlab/ref/eval.html

Code:
```x = 1 : 2 : 5;
y_x = input ('Enter your function of x = ','s');
y_eval = eval(y_x);
S = 2 .* sum(abs(y_eval))
plot( x , y_eval)
title(['Numeric Integration for ',num2str(y_x),' with 2 slices'])```
You can do something like the above. Its a simple change once you use the eval() function.

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:
Updated: March 25, 2013
Today on TSR

### University open days

1. University of Cambridge
Wed, 26 Sep '18
2. Norwich University of the Arts
Fri, 28 Sep '18
3. Edge Hill University
Faculty of Health and Social Care Undergraduate
Sat, 29 Sep '18
Poll
Useful resources

### Maths Forum posting guidelines

Not sure where to post? Read the updated guidelines here

### How to use LaTex

Writing equations the easy way

### Study habits of A* students

Top tips from students who have already aced their exams