Plotting using the Bar Function

2 vues (au cours des 30 derniers jours)
Calil
Calil le 27 Août 2022
Modifié(e) : dpb le 30 Août 2022
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 commentaires
dpb
dpb le 27 Août 2022
Modifié(e) : dpb le 27 Août 2022
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 le 27 Août 2022
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.

Connectez-vous pour commenter.

Réponses (2)

Image Analyst
Image Analyst le 27 Août 2022
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 commentaire
Image Analyst
Image Analyst le 27 Août 2022
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.

Connectez-vous pour commenter.


Cris LaPierre
Cris LaPierre le 28 Août 2022
Modifié(e) : Cris LaPierre le 28 Août 2022
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 commentaires
Cris LaPierre
Cris LaPierre le 30 Août 2022
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 le 30 Août 2022
Modifié(e) : dpb le 30 Août 2022
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...

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by