i don't know code

2 views (last 30 days)
Ahmad on 20 Mar 2023
Answered: Walter Roberson on 20 Mar 2023
hay i'm begginer in matlab
so i put them like this:
>> x=[10 20 30 40 50 60 70];
>> f(x)=1./(cosd(x)+2*sind(x)).^2;
>> g(x)=(sind(x).^3)./((x.^2)+3);
now i don't know how to write f(g(x)) in matlab
can you gyus help me? 🥺

Answers (2)

Dyuman Joshi
Dyuman Joshi on 20 Mar 2023
Edited: Dyuman Joshi on 20 Mar 2023
Define f and g as function handles, and use x and the output of g(x) as inputs respectively -
%Using different variable name to avoid confusion
in = [10 20 30 40 50 60 70];
f = @(x) 1./(cosd(x)+2*sind(x)).^2;
g = @(x) sind(x).^3./((x.^2)+3);
format long
G = g(in)
G = 1×7
1.0e-03 * 0.050836245147551 0.099277311533851 0.138427464008859 0.165679573498936 0.179597815557025 0.180271732677860 0.169237092716588
FoG = f(G)
FoG = 1×7
0.999996450970805 0.999993069175195 0.999990336015794 0.999988433492457 0.999987461834887 0.999987414787557 0.999988185135481

Walter Roberson
Walter Roberson on 20 Mar 2023
x=[10 20 30 40 50 60 70];
That is a numeric vector.
The right-hand side calculates a vector of specific values based on the content of the numeric vector -- so the first result is the calculation when x is 10, the second result is the calculation when x is 20, and so on.
The left-hand side is a currently-unknown name, brackets, a vector of positive integer values. In MATLAB, that is a request to create a new variable with the given name, and index the variable at the given locations and assign the results from the right hand side. WIth the first element of x being 10, then f(10)=the first calculation result. f(1) through f(9) would be assigned zero automatically and f(10) would be filled in with values. Then the second x is 20, so f(20)=the second calculation result. f(11) through f(19) would be assigned zero automatically, and f(20) would be assigned the second result. At the end you end up with a vector with max(x) = 70 elements, most of which are zero, but the 10'th, 20'th, 30'th and so on are assigned values calculated on the right hand side of the assignment.
The same kind of situation applies for your g(x) assignment.
In f(g(x)), the g(x) part would recall those calculated values from the second assignment, and try to use them as indexes into numeric variable f. Which would fail because the g values are not positive integers.
You have made the common mistake of confusing formulas and indexing.
In MATLAB, there are two ways to write formulas:
  • if you have the Symbolic Toolbox, you can write code such as syms x; f(x) = 1./(cosd(x)+2*sind(x)).^2 which would create a symbolic function named f with a placeholder x inside it . If you were to later evaluate f(10) for example then the 10 would be substituted for the x in the formula and a symbolic numeric result would be calculated. You can build g(x) the same way and you can calculate f(g) getting out a new symbolic function, which you can afterwards evaluate on specific values
  • The Symbolic Toolbox is not always available, and although it tries to be very precise, sometimes you do not need full indefinite precision -- sometimes you just don't care what the 37'th digit of sind(10) is, for example. In such cases you can build anonymous functions.
f = @(x) 1./(cosd(x)+2*sind(x)).^2;
is an example of an anonymous function. It is quite similar to defining
function result = f(x)
result = 1./(cosd(x)+2*sind(x)).^2;
but has some important differences that are not relevant at the moment.
You can also define
g = @(x) (sind(x).^3)./((x.^2)+3);
but if you were to now ask for f(g) you would get an error, not a compound formula like you could get from Symbolic Toolbox. When you evaluate an anonymous function then [for the purposes of the current discussion] you need to pass in specific numeric values. So you could call
f(g([10 20 30]))
because the g([10 20 30]) would return specific numeric values that then got passed to f for evaluation, getting out specific numeric values. But you would not be able to see the formula 1/(cos((pi*sin((pi*x)/180)^3)/(180*(x^2 + 3))) + 2*sin((pi*sin((pi*x)/180)^3)/(180*(x^2 + 3))))^2 like you could if you used the Symbolic Toolbox


Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!