MATLAB Answers

their something wrong with my script need help please

145 views (last 30 days)
Mutaz Alsomali
Mutaz Alsomali on 5 Jul 2018
Edited: Guillaume on 21 Jan 2020
%the dewpoint function
function Td= dewpoint(T, RH)
%set the a and b values
a = 17.27;
b = 237.7;
%declare the
Td=[];
f=[];
for i=1:length(RH)
%compute f
f(i) = a.*T./(b + T) + log(RH(i));
%copute Td
Td(i) = b.*f(i)./(a-f(i));
end
end
%the my_plot function
function p=my_plots(Td, RH)
%plot and labeling
plot(Td, RH)
title('Mutaz alsomali Dew point plot')
xlabel('Td (F)')
ylabel('RH(%)')
end
%test case1
T=15
RH=0.4
Td= dewpoint(T, RH)
%test case 2
T=35
RH=0.8
Td= dewpoint(T, RH)
figure
%plot for 25
T=25;
RH=0.1:0.05:0.95;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
%plot for 32F
T=(32-32).*5./9;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
%plot for 77F
T=(77-32).*5./9;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
%plot for 95F
T=(95-32).*5./9;
Td= dewpoint(T, RH);
my_plots(Td, RH);
legend('25 C', '32F', '77F', '95F')
hold off

  2 Comments

Guillaume
Guillaume on 5 Jul 2018
You should tell us what is wrong with your script rather than letting us guess. If you get an error, then give us the full text of the error message (everything in red). If you don't get the result you expected then what did you expect and what did you get instead?
Mutaz Alsomali
Mutaz Alsomali on 5 Jul 2018
Error: File: dewpoint.m Line: 29 Column: 1 Function definitions in a script must appear at the end of the file. Move all statements after the "dewpoint" function definition to before the first local function definition.

Sign in to comment.

Accepted Answer

Jan
Jan on 5 Jul 2018
Edited: Jan on 5 Jul 2018
The error message is clear already and contains the required instructions:
Function definitions in a script must appear at the end of the file.
Move all statements after the "dewpoint" function definition to before
the first local function definition.
Did you try this already?
%test case1
T=15
RH=0.4
Td= dewpoint(T, RH)
%test case 2
T=35
RH=0.8
Td= dewpoint(T, RH)
figure
%plot for 25
T=25;
RH=0.1:0.05:0.95;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
%plot for 32F
T=(32-32).*5./9;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
%plot for 77F
T=(77-32).*5./9;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
%plot for 95F
T=(95-32).*5./9;
Td= dewpoint(T, RH);
my_plots(Td, RH);
legend('25 C', '32F', '77F', '95F')
hold off
function Td = dewpoint(T, RH)
%set the a and b values
a = 17.27;
b = 237.7;
%declare the
Td= zeros(1, length(RHS)); % PRE-ALLOCATE !!!
f=zeros(1, length(RHS)); % PRE-ALLOCATE !!!
for i=1:length(RH)
%compute f
f(i) = a.*T./(b + T) + log(RH(i));
%copute Td
Td(i) = b.*f(i)./(a-f(i));
end
end
%the my_plot function
function p=my_plots(Td, RH)
%plot and labeling
plot(Td, RH)
title('Mutaz alsomali Dew point plot')
xlabel('Td (F)')
ylabel('RH(%)')
end
I've simple moved the script part of the file to the top as explained in the message.
By the way: Try to write meaningful comments. "%set the a and b values" is very obvious, but where do the values come from? "%declare the" is not useful and "%the my_plot function" is neither. Comments will save your live as programmer. While an exhaustively commented code can be expanded, maintained and debugged, missing comments leaves code in a status, in which deleting and rewriting is the most reliable option.
Pre-allocation is much more efficient than letting an array grow iteratively.

  3 Comments

Mutaz Alsomali
Mutaz Alsomali on 5 Jul 2018
i did that before and it showed me this: "Error: File: dewpoint.m Line: 38 Column: 15 Function with duplicate name "dewpoint" cannot be defined." so a had to made the arrangement that i put in display and it still make an error for some reason.
Mutaz Alsomali
Mutaz Alsomali on 5 Jul 2018
thank you for your comment and ill keep your advice in mind
Jan
Jan on 5 Jul 2018
It is safer to work with functions than with scripts. Scripts can modify variables in workspace of the caller, which can be confusing also.
I'm happy that I could help you.

Sign in to comment.

More Answers (1)

Andrey Porokhnyuk
Andrey Porokhnyuk on 21 Jan 2020
Edited: Andrey Porokhnyuk on 21 Jan 2020
I am moving from Octave, and these restrictions in Matlab are very confusing.
Simply, I had multiple functions in a complex script. When I moved all of them to the end, the interpretter was still throwing this senseless error.
The below-like script throws the same error:
"Error: File: script.m Line: 13 Column: 4
Function definitions in a script must appear at the end of the file.
Move all statements after the "fund" function definition to before the first local function definition.
%% the script code
a=[];
b=[];
c=fund(a,b);
a=fune(a,c);
%functions below
func = @(x, varargin) x(varargin{:});
function a=fund(b,c)
%some code
end;
function a=fune(b,c)
%some code
end;
So, what do you think?
";"
Semicolon!
Matlab has got indigestion from simple semicolon, indicating the silent end of instruction.
No, really, why is it so picky? It is not C++, where {curvies} contain the definition. There are "starting word", and "finishing word" for one declaration/definition. Since it looks like any other instruction, why doesn't it accept semicolon?

  1 Comment

Guillaume
Guillaume on 21 Jan 2020
You would be much better off reposting this as a new question. At the very least, it should have been posted as a comment to the question. It's certainly not an answer.

Sign in to comment.

Sign in to answer this question.