writting down the code for the function
이전 댓글 표시
Attached file has the function and here is my matlab code pasted below
function y = myFunction(x, n)
if n == 1
y = 4 * sin(x) / (25*x + sin(25));
else
y = 2 * tan(5*n) / bi;
end
end
% Define the values of x and n
n = 2;
% Call the function to calculate y
y = myFunction(x, n);
% Display the result
disp(y);
답변 (1개)
Walter Roberson
2024년 2월 7일
y = 4 * sin(x) / (25*x + sin(25));
That is not vectorized. It needs to be
y = 4 * sin(x) ./ (25*x + sin(25));
Also remember that the parameters to the trig functions are in radians . If you want degrees, use sind
댓글 수: 9
Walter Roberson
2024년 2월 7일
y = 2 * tan(5*n) / bi;
bi is not defined.
Walter Roberson
2024년 2월 7일
You are attempting to define variables after the end of the function. You need to put the script first
... but you have the problem that you do not define x before attempting to pass x on the call.
% Define the values of x and n
n = 2;
% Call the function to calculate y
y = myFunction(x, n);
% Display the result
disp(y);
function y = myFunction(x, n)
if n == 1
y = 4 * sin(x) / (25*x + sin(25));
else
y = 2 * tan(5*n) / bi;
end
Walter Roberson
2024년 2월 8일
zeta_n is not defined when it is used.
The sum() kind of implies that zeta_n is a vector.
Bijaya
2024년 2월 8일
n is also not defined
Remember that sin(25) is sine of 25 radians.
It is confusing that you syms Bi and then define Bi in terms of Bi
You subs(F, x, x_value) but F is not defined in terms of x.
% Define the functions
syms x Fo C_n Bi; % Declare symbolic variables
syms n
syms zeta_n [1 3]
F = sum(C_n .* exp(-zeta_n.^2 * Fo) .* cos(zeta_n)); % Function F(x)
Cn = 4 * sind(5 * C_n * n) + (25 * n + sind(25)); % Function Cn(n)
Bi = 5 * tand(5) + Bi; % Function Bi
% Define the values for constants
C_n = 1; % Constant C
Fo = 2; % Constant Fo
Bi = 3; % Constant Bi
% Define values for variables
x_value = 0:0.1:10; % Values of x from 0 to 10
n_value = 1:10; % Values of n from 1 to 10
% Calculate F(x) for given x values
F_values = subs(F, x, x_value);
% Calculate Cn(n) for given n values
Cn_values = subs(Cn, n, n_value);
% Calculate Bi
Bi_value = subs(Bi);
% Display results
disp('Values of F(x):');
disp(F_values);
disp('Values of Cn(n):');
disp(Cn_values);
disp(['Value of Bi:', num2str(Bi_value)]);
Bijaya
2024년 2월 8일
Bijaya
2024년 2월 8일
Walter Roberson
2024년 2월 8일
Bi_value is symbolic, not numeric. You cannot num2str() it. You can char() it.
카테고리
도움말 센터 및 File Exchange에서 Symbolic Variables, Expressions, Functions, and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

