I need some help solving numerical integration.
The integral is:
fun = @(x) (x .* (exp(x) ./ (exp(x) - 1).^2) .* ((asind(0.0003 .* T .* x)).^2 ./ sqrt(1 - (0.0003 .* T .* x).^2))); Note: asind = sin^-1(....) --> (I am not sure if this is correct)
C = @(T) integral(fun, 0, 517.39 ./ T);
The limit of x is from: x = 0 to x = (500/T)
T ranges from (1 - 1000) (which is T = logspace(0, 3))
Can anyone help me solve this integral. I want to plot a loglog plot of (C as a function of T). I am getting some errors which I am not able to overcome.
Thanks in advance.
Raj.

 채택된 답변

Star Strider
Star Strider 2020년 10월 13일

1 개 추천

See my Answer to your duplicate Question: Numerical Integration In Matlab

댓글 수: 4

Francesco Tricarico
Francesco Tricarico 2020년 10월 13일
I didn't see it's a duplicate question. What do you think about adding the arrayfun alternative? You can add it to your code in your exhaustive answer in the other thread, if you want.
Bye,
Francesco
Star Strider
Star Strider 2020년 10월 13일
Francesco — I voted for your Answer because I applaud your effort and I want to encourage you to continue to contribute here.
Welcome to MATLAB Answers!
The arrayfun function is a for loop in disguise! It is appropriate in some situations, however it is slower and less-efficient than an explicit for loop, the reason I did not use it in my Answer. It works here, likely because this is a ‘one-off’ and its inefficiencies are likely not a consideration. It would be best to avoid using it with large data sets or other complications where speed and efficiency are significant considerations.
It would be appropriate to use it in an anonymous function, since in avoiding the need for an explicit for loop, it avoids the need to write a specific function file to do the same thing with a for loop.
As with everything else, coding depends on what you want to do and how you want to do it.
Francesco Tricarico
Francesco Tricarico 2020년 10월 13일
편집: Francesco Tricarico 2020년 10월 13일
I was ridiculous to suggest you my tip, ahah, maybe my sixth sense was hoping for a precious observation and this is what i've got by you. Thanks, Star.
Recently i began to face "long time to run" code in MATLAB, and so i'm getting interested in code efficiency. Although you've been very clear, I've tried to get information about arrayfun directly from MATLAB to see how it works closely but i'm not able because it's a buitl-in function.
Bye,
Francesco
Star Strider
Star Strider 2020년 10월 13일
See if the type or edit functions will let you see the inner workings of arrayfun. I never l;ooked, because I rarely use it.
As for ‘giving away tips’, most Answers here are minor variations on the same theme, and frequently identical code (in which case subsequent duplicate Answers are usually deleted by the author unless they offer unique perspectives). So using arayfun (intended to vectorise a for loop) occurred to me, however since the explicit for loop is almost always more efficient, I went with it instead in my Answer.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Francesco Tricarico
Francesco Tricarico 2020년 10월 13일

1 개 추천

Dear Raj,
To improve code readibility, i suggest to use the the code form when you post a message in MATLAB Central.
Here's is the working version of your commands:
fun = @(x,T) (x .* (exp(x) ./ (exp(x) - 1).^2) .* ((asind(0.0003 .* T .* x)).^2 ./ sqrt(1 - (0.0003 .* T .* x).^2)));
C = @(T) integral(@(x)fun(x,T),0,517.39./T);
You forgot to declare T as a variable in the fun definition and in the C the problem is direct consequence of that oversight.
Bye,
Francesco

댓글 수: 2

Ah,
to evaluating C for an array of values, i recommand to use arrayfun in that case, instead of redefine fun this aim.
For example, use that lines after the commands in my previous message:
X = logspace(0,3);
Y = arrayfun(C,X);
plot(X,Y)
Hope it works!
Raj Patel
Raj Patel 2020년 10월 13일
Thank Francesco.
Raj Patel.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2020년 10월 13일

댓글:

2020년 10월 13일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by