clc;
clear all;
close all;
c=3*10.^8;
h=6.625*10.^-34;
k= 1.38*10.^-23;
T=500;
f=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
R= ((2.*h).*(f.^3))./((c.^2).*(exp.^(h.*f/(k.*T))-1));
plot(f,R)

댓글 수: 1

John D'Errico
John D'Errico 2020년 8월 6일
편집: John D'Errico 2020년 8월 6일
Funny. I was sure this was a question about R (the language), not MATLAB. But it R a MATLAB problem (sorry.) In fact, I think this gets at a fundamental issue with MATLAB, and possibly other programming languages too, when new users try to use them.
Functions like exp are just second nature to me. I assume that most languages have an exp tool, something designed to compute e^x and to do so directly. If I were to use a spreadsheet, for example, I'd expect to find it, or Python, etc. Yeah, I'd need to include it in python. Still...
So often I see people having problems with exp. I'll see them doing things like
e = exp(1);
x = e^x;
thus, effectively two lines of code to perform something that will be far more efficiently done in one call to exp.
Or, I'll see someone do as was done in this question, trying to use exp as the constant e itself.
Or, I'll see someone write just
x = e^x
expecting MATLAB to know what was intended.
Interestingly, I did not even see an mlint flag appear when I put this line:
A = exp.^2;
into the editor, even though it will fail of course when I try to run the code.
Error using exp
Not enough input arguments.
So is there some way of making these tools more accessible to the new users?
Should that line of code at least have generated an flag in the editor, as possibly something questionable? Yes, I do understand that it is perfectly legal to define a variable named exp, if the user wants to do so. It is arguably a dangerous coding style, but that would be their choice to make.

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

 채택된 답변

Star Strider
Star Strider 2020년 8월 6일

1 개 추천

The problem is in the way you call the exp function.
This works:
R = ((2.*h).*(f.^3))./((c.^2).*(exp(h.*f/(k.*T))-1));
.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

태그

질문:

2020년 8월 6일

편집:

2020년 8월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by