• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

MATLAB: for loops and while loops

abracadabra1

Diamond Member
Hey there fellas.

I'm stumbling on this problem in MATLAB. I've been trying to create two distinct *.m files, one with a for loop and another with a while loop to run the operation, but it just doesn't seem to work. I get matrix dimension problems, missing operators, and all sorts of problems. Any help would be greatly appreciated. Here's the problem:

4) Use MatLab to produce a table of values of the functions :x sqrt(x) x^4 1/(1+x^3) exp(x) x^8/factorial(8) and k(x)
where you will define k(x) in an m-file to be the smallest strictly positive whole number n such that x^(2*n)/factorial(2*n) is less than
1e-4. The x values to use are 0 0.2 0.4 0.6 and so on to 3.

Thanks!
 
This is what I've come up with. I think it's correct, but am unsure.

n=0
x=0
for x=0:.2:3
k=x^(2*n)./factorial(2*n)
while k>1e-4
n=n+1
x
k=x^(2*n)./factorial(2*n)
if k>1e-4
n=0
end
end
[x,sqrt(x),x^4,1/(1+x^3),exp(x),x^8/factorial(8),k]
end
 
Not that you can't find someone here to help you but I think you'll have
better luck at the Matlab Newsgroup ->news://comp.soft-sys.matlab (can't seem to able put it on a link!)

Anyway you code works...
the last result x=3 is:
3.0000 1.7321 81.0000 0.0357 20.0855 0.1627 0.0001

What you need to do to get that table is to store the result in another variable...
the line before the last end could be something like:
y(end+1,: )=[x,sqrt(x),x^4,1/(1+x^3),exp(x),x^8/factorial(8),k] ;

And you should add semicolons to the end of every assignment line like the one above or else everything is echoed
back to the console....
For faster execution you can also inicialise the y variable before the for loop!!

Hope it helps!!



 
Back
Top