Does anyone know what i am doing wrong here. i am trying to plot my amplitudes of my fourier series from 0-15 my bn is only a number when odd which explains my matrix.

 채택된 답변

Torsten
Torsten 2024년 1월 2일
이동: Torsten 2024년 1월 2일

1 개 추천

Use elementwise division (./) and multiplication (.*) in the computation of An and B:

추가 답변 (1개)

Dyuman Joshi
Dyuman Joshi 2024년 1월 2일
편집: Dyuman Joshi 2024년 1월 2일

1 개 추천

The element-wise operators to be used here are ./ and .* (There is .^ in addition), see - Array vs Matrix Operations
Also, while defining the variable C - the letters a, b of An, Bn are supposed to be uppercase, and, n of plot C vs N should be lowercase. MATLAB is case sensitive.
And the syntax of calling plot() is incorrect. Take a look at the documentation page to learn the acceptable syntaxes of the function plot() here - https://in.mathworks.com/help/matlab/ref/plot.html
If you are new to MATLAB, I suggest you take the free MATLAB Onramp tutorial to learn the fundamentals/basics of MATLAB along with the notation/syntax.

댓글 수: 9

AB29
AB29 2024년 1월 2일
Thank you the response. I have modified my answer however now my graph is blank.
Why is that?
You need to directly use n -
n = 1:2:15;
A = 3;
An = (A./(pi.*n)).*(sin(pi*n) - sin(0));
Bn = (-A./(pi.*n)).*(cos(pi*n) - cos(0));
C = sqrt(An.^2 + Bn.^2);
plot(n, C)
AB29
AB29 2024년 1월 2일
Amazing thanks. I am now doing the same with these values (below). However I am struggling to get matlab to recognise that the figures in the square root are negative. From hand calculations i get a1 = 0.569 (rounded) and a2 = 0.546 (rounded). Matlab is way off because it doesn't take into account the minus. Any ideas?
Dyuman Joshi
Dyuman Joshi 2024년 1월 2일
Since sum of squares is positive, thus its sqrt() is also positive.
Please state/share -
> What the expected output is.
> The mathematical formula/expression that you are trying to code for An and Bn.
Steven Lord
Steven Lord 2024년 1월 2일
Since you're computing the sine and cosine of multiples of pi, I'd use the sinpi and cospi functions instead of sin and cos. Or if you're trying to compute degree-based sine and cosine, use sind and cosd instead of trying to convert the angles in degrees to radians manually. As for your last calculation, C, use hypot.
AB29
AB29 2024년 1월 2일
This is what i expected
Torsten
Torsten 2024년 1월 2일
편집: Torsten 2024년 1월 2일
C1 = sqrt(0.908^2 + (-0.705)^2)
C1 = 1.1496
C2 = sqrt((-0.281)^2 + (-0.614)^2)
C2 = 0.6752
C3 = sqrt((-0.187)^2 + (-1.26)^2)
C3 = 1.2738
Dyuman Joshi
Dyuman Joshi 2024년 1월 3일
@AB29, the signs should be positive, as squaring a negative value results in a positive value.
AB29
AB29 2024년 1월 3일
Thanks all.

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

카테고리

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

제품

릴리스

R2023b

질문:

2024년 1월 2일

댓글:

2024년 1월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by