You are Here: Home >< Maths

# MATLAB, writing conditional statements watch

1. f(x) = { 2x^2 for -6 ≤ x ≤ -2
e^3x for -2 ≤ x ≤ 2.5
ln(2x) for 2.5 ≤ x ≤ 6 }

(this is just a made up example question, so that you're not helping me directly do my coursework ^^)

so. I need to use a CONDITIONAL STATEMENT in MATLAB to evaluate these ...

roughly how do i do it XD

Thanks!! I will rep the first most helpful anser at 00:00
2. Get yourself an m file, and put in something like this:
x=input (‘Enter a value for x - ’)
while x>=-6 && x<=-2
f(x)= 2x^2
if x>=-2 && x<=2.5
f(x)=e^3*x
elseif x>=2.5 && x<=6
f(x)=ln(2*x)

Possibly? I'm a matlab noob and don't have it on this computer...haha.
3. (Original post by Kittyyyyyyy)
Get yourself an m file, and put in something like this:
x=input (‘Enter a value for x - ’)
if x>=-6 && x<=-2
f= 2*x^2
elseif x>=-2 && x<=2.5
f=e^3*x
elseif x>=2.5 && x<=6
f=ln(2*x)
else
f=0
end

Possibly? I'm a matlab noob and don't have it on this computer...haha.
Just fixed the parts in bold for you. The WHILE loop isn't necessary, and you were missing the 'end' from the IF loop. I also added the additional statement at the end that will make f=0 if x is outside the values defined and made the variable f where you put f(x). Putting f(x) willl tell matlab to put the value into index x in array f, which isn't what you want to do.
4. lol i've no idea who to give the rep to at mid night

### Related university courses

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: February 14, 2010
Today on TSR

How do you think you'll do?

### University open days

Wed, 25 Jul '18
2. University of Buckingham
Wed, 25 Jul '18
3. Bournemouth University
Wed, 1 Aug '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