Is there any error in my code?

조회 수: 9 (최근 30일)
LAWRENCE DUMEBI
LAWRENCE DUMEBI 2017년 6월 9일
편집: John BG 2017년 6월 11일
These are my codes to plot the graph of f(x, q) = (q^4/(q^3+6))(1+x^3)exp(-qx)
clear all
q = 0.4;
x = linspace(0,20);
for i = 1:length(x);
f(i) = (q^4/(q^3+6))*(1+x(i))*exp(-q*x(i));
end
plot(x,f,'linewidth',2)
xlabel(x)
ylabel('f(x)')
please reply me with the correction. Thanks.
  댓글 수: 2
Adam
Adam 2017년 6월 9일
편집: Adam 2017년 6월 9일
You are missing a ^3 from your code, but this really is something you should double-check yourself before asking other people!
You don't need a for loop either, you can just vectorise it, but if at the moment you are only at the level of for loops I will leave that aside.
You can also easily create a quick test script with correct results calculated by hand or by calculator to check whether your answer is right or not.
Jan
Jan 2017년 6월 9일
I've used the "{} Code" button to format your code, which was unreadable. Please do this by your own in the future - thanks.

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

채택된 답변

John BG
John BG 2017년 6월 11일
편집: John BG 2017년 6월 11일
Hi Lawrence
even better, since you are after f(x,q) perhaps a 2D plot is more helpful
q = [0:.1:4];
x = linspace(0,20);
[Q,X]=meshgrid(q,x)
F = (Q.^4 ./ (Q.^3 + 6)) .* (1 + X.^3) .* exp(-Q .* X);
surf(F)
.
.
So Lawrence, if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG

추가 답변 (1개)

Jan
Jan 2017년 6월 9일
편집: Jan 2017년 6월 9일
The ^3 in (1+x^3) has been forgotton:
... (1+x(i)^3) ...
You can do this without a loop also:
x = linspace(0,20);
f = (q^4 / (q^3 + 6)) * (1 + x.^3) .* exp(-q * x);

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by