There is something wrong in R.

1 view (last 30 days)
rajdeep singh
rajdeep singh on 6 Aug 2020
Edited: John D'Errico on 6 Aug 2020
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 Comment
John D'Errico
John D'Errico on 6 Aug 2020
Edited: John D'Errico on 6 Aug 2020
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.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 6 Aug 2020
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));
.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!