Plotting using the Bar Function

I'm trying to plot a bar function in matlab. but it states that my y1 variable is incorrect. Can somebody please tell the error in my syntax?

댓글 수: 2

dpb
dpb 2022년 8월 27일
편집: dpb 2022년 8월 27일
Not sure what the grader is complaining about -- I'm far too old to have ever been exposed to one of these so don't personally know anything about the metrics they use...only things that come to me are
  1. Used y1 as the variable instead of y which was given in the problem -- that's perfectly legitimate code but maybe it's picky about matching the actual variables in the problem??
  2. There are extra parentheses in the expression for y1 that aren't needed -- again, there's nothing functionally wrong, but perhaps it's picky there, too???
  3. There are unneeded "dot" operators besides the one that is needed -- agains, there's nothing functionally wrong, but perhaps....???
y=exp(x/2).*sin(2*x);
would be about as cleanly written as possible; operation on vectors/arrays by constants is handled by MATLAB syntax with implicit expansion without the need for the dot operatior and there's no need to surround terms with parentheses unless needed in complex operations to ensure proper grouping -- like a term such as 1+x/2 performs the division before the addition by operator precedence so if (1+x)/2 is intended, then the parentheses are needed. Nothing like that in the above expression.
So, while technically correct, it appears the grader is also either just comparing to a known/defined pattern or is checking for such stylistic issues as well, not just looking at whether the answer agrees numerically with known results.
>> y1=exp((1/2).*(x)).*sin(2.*x);
>> all(y==y1)
ans =
logical
1
>>
shows the results are identical either way.
Steven Lord
Steven Lord 2022년 8월 27일
Please post the full and exact text of the message you receive from the grader. Knowing exactly what it suggests is incorrect may be useful in determining how to correct the problem.
If I had to guess I'd say it was complaining that you haven't added the legend etc. as requested in the sentence starting with "Be sure" but with the message I probably wouldn't have to guess.

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

답변 (2개)

Image Analyst
Image Analyst 2022년 8월 27일

0 개 추천

Your code seemed to work for me. What error did it give you? Reply after you read this:
x = linspace(0, 10, 100);
y1 = exp((1/2) .* x) .* sin(2 * x);
bar(x, y1)
grid on;
title('My Homework')
xlabel('x');
ylabel('y');
legend

댓글 수: 1

What is "it"? Is it MATLAB? Or is it some kind of automated grader program like @dpb mentioned? He might be right that the grader might look for all the requested functions and look for specific variable names, like y instead of y1. So try
x = linspace(0, 10, 100);
y = exp((1/2) .* x) .* sin(2 * x);
bar(x, y)
grid on;
title('My Homework')
xlabel('x');
ylabel('y');
legend
If it's still not working show us EXACTLY what "it" is reporting. We need ALL the error text, not just a paraphrasing of it or a snippet from it.

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

Cris LaPierre
Cris LaPierre 2022년 8월 28일
편집: Cris LaPierre 2022년 8월 28일

0 개 추천

The error you are getting from Grader is that the values of y1 are incorrect (if you were using the wrong variable name, the error would istead tell you the solution was expected to have a variable named 'y1'). This was determined by comparing the values of your variable with those generated by the instructor's solution (with some tolerance applied). So it is not that your code has an error, it is that you are not getting the expected result.
Where the error is not obvious, you may consider reaching out to your instructor if you think there is an error in the grading. You can do so by following the steps in this Answer and sending the solution id along with an explanation to your teacher.

댓글 수: 11

dpb
dpb 2022년 8월 28일
Well, indeed if the problem being graded is the one posted and there's a difference in the result it would surely seem it's the grader version that's in error, @Cris LaPierre -- certainly his solution meets the specification given.
I was able to generate another answer that matches the problem specification by using the internal difference in MATLAB between how colon and linspace deal with rounding by using
x2=0:0.1*100/99:10;
instead, but the answers are still within roughly double precision eps() although the functional is
>> [mx,imx]=max(abs(y1-y))
mx =
1.8474e-13
imx =
83
>> y1(imx)
ans =
-47.5672
>> mx/eps(ans)
ans =
26
>>
some 26X eps() at that point so if Grader were to use something less than that as a tolerance I suppose it's possible. I'd think that a bum result for the student if something like that causes a failing or discounted result, though.
I can't think of a way to generate anything that would be grossly different without some really bizarre operations involved, maybe -- and don't really have any ideas on what those might be that could use.
Cris LaPierre
Cris LaPierre 2022년 8월 29일
Default tolerances in Grader are +/- 0.0001 absolute or +/-0.1% relative. These can be adjusted by the problem author, of course, but I suspect that is unlikely here.
To @Calil, have you shared all your solution code? Do you perhaps modify the values of y1 after the plot is created?
dpb
dpb 2022년 8월 29일
"...perhaps modify the values of y1 after..."
Indeed, it would seem that such would be about the only way to produce a result outside those kinds of tolerances.
I suppose we'll probably never know; @Calil hasn't come back...
@Cris LaPierre -- what would Grader return as the error if the instructor didn't follow the outlined rules and wrote his code as
x=[0:0.1:10];
instead of using linspace or the artifice I showed above of 0.1*100/99 to only get 100 instead of 101 points? Would it just note the two don't match or would it flag the size difference? Or would that answer depend on what/how the instructor coded the grading and could be either?
Cris LaPierre
Cris LaPierre 2022년 8월 29일
The error is coming from the assessment test the instructor created, where the name of the variables to compare is specified. Assuming the test has been set up to check variable y1,
  1. It first checks that the indicated variable exists. If it does not, the error is "The submission must contain a variable named y1."
  2. If the variable exists, it next checks the data type. If it does not match the reference variable data type, the error is "Variable y1 must be of data type double. It is currently of type ____. Check where the variable is assigned a value."
  3. If the data type is correct, it next checks the variable size. If it does not match the reference variable size, the error is "Variable y1 must be of size [# #]. It is currently of size [# #]. Check where the variable is assigned a value."
  4. If the size is correct, it checks the value (with a tolerance). If any value is incorrect, the error message is "Variable y1 has an incorrect value."
Since the user reports getting error message #4, their variable has the expected name, data type and size, but at least one of the values is incorrect.
dpb
dpb 2022년 8월 29일
편집: dpb 2022년 8월 30일
We're pondering the imponderable here, of course, but interesting that y1 would be an expected variable name when the problem as posted by the OP is in terms of y(x), not y1(x).
We'll never know unless @Calil reports back to us at some point.
But, it's interesting and potentially useful going forward to have some idea how the grader functions as we see more and more of these on the forum.
Cris LaPierre
Cris LaPierre 2022년 8월 30일
It is the variable name in the code that matters, not the equation given in the problem description.
You can read more about the assessment function here: https://www.mathworks.com/help/matlabgrader/ug/assessvariableequal.html
dpb
dpb 2022년 8월 30일
Well, yes, but your first bullet said "Assuming the test has been set up to check variable y1,"
I was just commenting that would seem to be an unusual choice to set up that way when the problem description given the student used just y=f(x) -- what's the klew the poor student has to know to use y1 instead -- already tried y and got kicked in the teeth by the robot telling him "don't do what makes sense from the problem, do this instead?"
Granted, by your description of the logic tree above that it would barf on a missing variable name if not finding y1 would seem to make the assumption necessary to produce the result, but just seems strange to me (from the information posted; there well may be a lot more in the whole problem/assignment we're not seeing that would make it all perfectly clear).
Cris LaPierre
Cris LaPierre 2022년 8월 30일
The gray lines are locked lines, meaning the instructor presents students with a template that walks them through how to solve the problem. On line 3, the instructions tell the learners to create y1.
dpb
dpb 2022년 8월 30일
편집: dpb 2022년 8월 30일
Ah, so! I had presumed just student highlighted comments; didn't understand there being such a template.
Makes sense, other than the choice by the instructor of the variable name...and that the answer was judged to be wrong for a most inexplicable reason (again, only from what we've been able to see, of course).
Thanks for the tutorial, Cris; much appreciated and I have a lot better idea of how Grader functions that, as noted before, is bound to help later Q? responses...

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

카테고리

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

태그

질문:

2022년 8월 27일

편집:

dpb
2022년 8월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by