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!

