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);

Antworten (1)

Walter Roberson
Walter Roberson am 7 Feb. 2024

0 Stimmen

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 Kommentare

y = 2 * tan(5*n) / bi;
bi is not defined.
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
so i did went ahead and modify my code using symbolic tool and keep on getting error all the time . Here is my pasted code below for the same problem
% Define the functions
syms x Fo C_n Bi; % Declare symbolic variables
F = sum(C_n .* exp(-zeta_n.^2 * Fo) .* cos(zeta_n)); % Function F(x)
Unrecognized function or variable 'zeta_n'.
Cn = 4 * sin(5 * C_n * n) + (25 * n + sin(25)); % Function Cn(n)
Bi = 5 * tan(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)]);
Walter Roberson
Walter Roberson am 8 Feb. 2024
zeta_n is not defined when it is used.
The sum() kind of implies that zeta_n is a vector.
Bijaya
Bijaya am 8 Feb. 2024
I did define zeta_n in the sym ones and these are the error from the matlab
Error using sym/subs
Entries in second argument must be scalar.
Error in untitled4 (line 20)
Cn_values = subs(Cn, n, n_value);
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):');
Values of F(x):
disp(F_values);
disp('Values of Cn(n):');
Values of Cn(n):
disp(Cn_values);
disp(['Value of Bi:', num2str(Bi_value)]);
Error using num2str
Input to num2str must be numeric.
Bijaya
Bijaya am 8 Feb. 2024
what is the deal with numstr . I keep getting same error too
Bijaya
Bijaya am 8 Feb. 2024
The above modified one leads to following errors
Error using num2str
Input to num2str must be numeric.
Error in untitled523 (line 26)
disp(['Value of Bi:', num2str(Bi_value)]);
Walter Roberson
Walter Roberson am 8 Feb. 2024
Bi_value is symbolic, not numeric. You cannot num2str() it. You can char() it.

Melden Sie sich an, um zu kommentieren.

Tags

Gefragt:

am 7 Feb. 2024

Kommentiert:

am 8 Feb. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by