I dont know how to solve the below question. I'm completely lost on where to even begin. I understand the fprintf, just the sin and cos always throw me off. Any direction would even be greatly appreciated

조회 수: 1 (최근 30일)
For 0 ≤ x ≤ 1 with an increment of 0.1 in x, compute f( x )=sin*x/1+x. For this computation, set x=0:0.1:1 and then perform element-by-element multiplication (.*) operation. Please avoid plugging each x to the function. Using fprintf, print out x and f(x) in two columns with each variable having 8 decimal places.
  댓글 수: 2
Adam
Adam 2018년 6월 4일
편집: Adam 2018년 6월 4일
f( x )=sin*x/1+x
is a highly suspicious definition of a function. For a start sin has no argument. I assume it should be sin( x ). Also I assume the
1 + x
should be
( 1 + x )
otherwise you are just dividing by 1 (pointless) then adding x to the result.
Apart from that I don't really understand what your confusion is though. If you don't know what sin is then that is a very basic maths problem that you need to look up in a book or online. Assuming you do know what sin is then
doc sin
will take you to the Matlab help page on the function that implements sine.
Most of the rest of it is explicitly stated as to how to do it.
James Niemiec
James Niemiec 2018년 6월 4일
So what I have completed done so far is like such
  • X = ( 0 : 0.1 : 1) ' ;
  • X1= sin(X);
  • F_x=((X1)/(1+x));
  • fprintf('Variable X: %1.8 f | Variable F(x): %1.8f', [X,F(x)].');
  • This causes my code to appear in two different columns like i want but with a lot of other random data in it.
  • Variable X = 0.00000000 | Variable F(x)= 0.00000000
  • Variable X = 0.00000000 | Variable F(x)= 0.00000000
  • Variable X = 0.00000000 | Variable F(x)= 0.00000000
  • Variable X = 0.00000000 | Variable F(x)= 0.00000000
  • Variable X = 0.00000000 | Variable F(x)= 0.00000000
  • Variable X = 0.00000000 | Variable F(x)= 0.00000000
  • Variable X = 0.10000000 | Variable F(x)= 0.00000000
  • Variable X = 0.00000000 | Variable F(x)= 0.04991671
  • It then repeats this cycle again 7 more times till Variable X=0.20000 and so on over and over again.

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

채택된 답변

monika shivhare
monika shivhare 2018년 6월 4일
x=0:0.1:1;
f=sin(x./(1+x));
for i=1:length(x)
fprintf('%0.8f %0.8f\n',x(i),f(i));
end
  댓글 수: 6
Rik
Rik 2018년 6월 4일
A for-loop in Matlab executes the block of code until the matching end, with i taking on the values of the columns of the supplied array. You don't have to actually use the loop index for the for-loop to work.
In this case, i will take on the values 1 through 11.
There is a big difference between for and while.
Stephen23
Stephen23 2018년 6월 4일
편집: Stephen23 2018년 6월 5일
"Many of the for statements ive dealt with in the past were used in C# or C++ coding"
Sure, but MATLAB is not C or C++. If you want to learn about MATLAB, then you will need to read the MATLAB documentation: for is described as "for loop to repeat specified number of times" (it does not say "to repeat forever"). The documentation then shows how to specify the values to loop over.

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

추가 답변 (0개)

카테고리

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

태그

아직 태그를 입력하지 않았습니다.

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by