d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Matlab Code/programing Need Help
123Next
Add Reply New Topic New Poll
Member
Posts: 239
Joined: Aug 27 2014
Gold: 0.01
Sep 12 2014 12:26pm
Write a function that converts calendar months (input as numbers, 1-12) to
seasons (output as numbers, 1-4). Months 12 and 1-2 are season 1 [winter], 3-5
are season 2 [spring], 6-8 are season 3 [summer], 9-11 season 4 [fall]. Use an
if. . . else statement. Have the function exit with an error if the input month
is not between 1 and 12. Include help text to tell users what the function does.
Check the function with a valid input (such as 7) and an invalid input (such as
0).


I have no clue how to start this problem, my matlab skills are weak. can someone help?
Member
Posts: 239
Joined: Aug 27 2014
Gold: 0.01
Sep 12 2014 01:26pm
this is wat I got so far..


function M = months(x)
if ( x==12 || x==1 || x==2 );
disp('1')
elseif ( x==3 || x==4 || x==5 );
disp('2')
elseif ( x==6 || x==7 || x==8 );
disp('3')
elseif ( x==9 || x==10 || x==11 );
disp('4')
else ( x<1 || x>12 );
disp('error, input month must be between 1 and 12')
end
end

but its not working, its giving me an error "undefined x or function" when I run months(3) in the command window
Member
Posts: 7,721
Joined: Oct 11 2008
Gold: 304.00
Sep 12 2014 01:28pm
Your code seems fine
you got 1 extra end
did you define x?

This post was edited by saber_x3 on Sep 12 2014 01:31pm
Member
Posts: 7,721
Joined: Oct 11 2008
Gold: 304.00
Sep 12 2014 01:38pm
since you're using a function, which you honestly shouldn't
you should change disp to M
and call M outside the function file
Code

if ( x==12 || x==1 || x==2 );
M=1
elseif ( x==3 || x==4 || x==5 );
M=2
Member
Posts: 239
Joined: Aug 27 2014
Gold: 0.01
Sep 12 2014 01:48pm
Quote (saber_x3 @ Sep 12 2014 03:28pm)
Your code seems fine
you got 1 extra end
did you define x?


Don't you need an "end" for the if statements and for the function itself?

do I define X in outside the function or inside?

Quote (saber_x3 @ Sep 12 2014 03:38pm)
since you're using a function, which you honestly shouldn't
you should change disp to M
and call M outside the function file
Code
if ( x==12 || x==1 || x==2 );
M=1
elseif ( x==3 || x==4 || x==5 );
M=2


well he said to write a function so I wrote it as function.
Member
Posts: 239
Joined: Aug 27 2014
Gold: 0.01
Sep 12 2014 01:55pm
I change it to this. And this is working as intended without defining X. I just ran "months(3)" in the command line and it displayed an output 2. Is this good enough to get full credit for #1?

THe professor wants us to write help text. Does he mean, write comments next to the code to indicate what it does? (ex: %this function converts months to season)

Code
function S = months(x)
if ( x==12 || x==1 || x==2 );
S=1 %Winter
elseif ( x==3 || x==4 || x==5 );
S=2 %Spring
elseif ( x==6 || x==7 || x==8 );
S=3 %Summer
elseif ( x==9 || x==10 || x==11 );
S=4 %Fall
else ( x<1 || x>12 );
disp('Error! Input month must be between 1 and 12!')
end
Member
Posts: 7,721
Joined: Oct 11 2008
Gold: 304.00
Sep 12 2014 01:58pm
you define x in your command or in your m file
then call on your function months(x)

Code
else ( x<1 || x>12 );
disp('error, input month must be between 1 and 12')

"( x<1 || x>12 )" can be left out
input month must be an integer 1 through 12

and yes, you need the end for the function file
I test ran the code online without the function

This post was edited by saber_x3 on Sep 12 2014 01:59pm
Member
Posts: 239
Joined: Aug 27 2014
Gold: 0.01
Sep 12 2014 02:01pm
Quote (saber_x3 @ Sep 12 2014 03:58pm)
you define x in your command or in your m file
then call on your function months(x)

Code
else ( x<1 || x>12 );
disp('error, input month must be between 1 and 12')

"( x<1 || x>12 )" can be left out
input month must be an integer 1 through 12

and yes, you need the end for the function file
I test ran the code online without the function


but do you need end for "if statements"
Member
Posts: 239
Joined: Aug 27 2014
Gold: 0.01
Sep 12 2014 02:23pm
I'm having trouble with 2a right now. I don't understand why my code is outputting numbers in that format nor do I know what the last line of output means. I know the fraction error is equal to |numerical answer = true answer| / |true answer|....|x - x*| / |x*|


Member
Posts: 7,721
Joined: Oct 11 2008
Gold: 304.00
Sep 12 2014 04:58pm
Ya, so it's giving you the correct answers, just negative
that is because you got b as 10^8 when it is -10^8
10^-8 is essentially 0
you can make matlab format it to show more zeros, but i don't remember how

matlab is matrix laboratory, and it expresses some answers as (constant*matrix)
it can help when you have a large matrix with large numbers
Go Back To Homework Help Topic List
123Next
Add Reply New Topic New Poll