easy Matlab question

Hades16x

2[H]4U
2FA
Joined
Sep 24, 2003
Messages
3,978
So I just started an ECE480 class, and this is the first time I've ever used Matlab... and I'm having a problem figuring this out.

I have to find an integral of x=(4*t.^2).*(.5.^t) from 0 to t1. I need to find t1.

So I understand how to integrate using matlab. One part of this lab was to integrate from 0 to infinity of this function.

t=[0:.01:100]';
x=(4*t.^2).*(.5.^t);
x=fun(t);
area=quad(@fun,0,100)

Knowing that I dont need to integrate to inf, I just used 100 as my upper limit.

Now with the problem I'm having..... integral of x,0,t1 is =16.16, so I must solve for to so that this is true.

It was recommended to use an if or a while loop to complete this, but I keep getting errors using them.

I know the answer because my trusty TI-89 can solve it in one line:
solve(integral(x,t,0,t1)=16.16) Is there a way I can do that with matlab or not?

Thanks for any help
Michael
 
I'm not sure whether or not I will be able to help you but I am a little confused by what you're actually solving for.

It is my understanding that you are to find the upper integration limit for the integral of that function. What criteria do you have to know when you've found the correct value for t1?

I may be misreading something but I don't quite understand how you are solving for t1.

-Matt
 
You're correct, Im solving for the upper integration limit. Im actually using to, but for the English translation of t(zero), I posted as t1.

t=[0:.01:10000]';
x=(4*t.^2).*(.5.^t);
x=fun(t) ;
area=quad(@fun,0,100)
eng=sum(x.^2)

areaans=quad(@fun,0,4.9996);
areaans

to=0
area1=quad(@fun,0,to)
while area1<=16.16
to=to+.0001
end
to;

If you run the areaans, which is the int of x from 0 to 4.9996, it equals 16.1602, which is close enough for the teacher.

I am terrible at programming, and my understanding of if and while loops is very poor.

What I tried to do in the loop was set to=0 for a starting point. Then I set the integral of x from 0 to (to) equal to area1. The I said, if area1 is less than or equal 16.16, increment to by .0001. I would assume that once area1 was greater than 16.16, the loop would stop.

Im assuming I have to have an else if statement, or even scrap the if statement and use a while. But neither seem to work.

Thanks,
Michael
 
Ok, I think I understand now. Let me see if I can help.

First, it seems that if you want to use '@fun' as an argument in 'quad' then you need a separate .m file called 'fun.m' with the following in it:

function y = fun(t)
x = (4*t.^2).*(.5.^t);

That will create a function that you can call using the quad command.

Second, when you made your while loop you are not actually calculating a new area when you increment 'to'.
It is an infinite loop and will run until 'to' is large enough to consume all of your memory. So add the following right inside the while loop:

area1=quad(@fun,0,to)

Lastly, remove the semicolon on 'to' in the last line or it will not display the value of to.
There are other ways to display values but that one will work in this case.

I hope this is clear and helps you out. You can disregard the single quotes in my text, I simply used that to denote Matlab arguments and variables.

-Matt
 
Yes, you're correct, I am using an .m file with that function.

Thank you, I knew I had something misplaced when to went >5. Like I said Im terrible with programming of any sorts so I didnt know I had to move the area1 into the while loop.

I silenced the to because I would pace around my room, come back 5 minutes later to see if I had a defined value of to. It was getting crazy how fast my screen was changing with values.

Thank you so much,
Michael
 
Not a problem, I'm glad to help. I guess having to use Matlab in a bunch of my ECE classes has paid off.

Remember, you still need to calculate the area before you enter the loop as well as each iteration of the loop.

-Matt
 
Yep, both inside and out.

Now onto creating unit pulses and convolution!
 
Back
Top