inline function and convert
2 ビュー (過去 30 日間)
古いコメントを表示
Question is that:
Create an m-file that asks for a function (sin(x) + cos(3*y) ....) that depends on different variables x,y and z or like the one in problem 1.Your m-file should localize y and z and then switch these against x. Plot the the new function versus x. Where x=0:0.1:20.
my code:
fnc=input('Enter any function with three variables:','s');
g=inline(fnc);
disp(g)
x=0:0.1:20;
plot(x,g,'r--','linewidth',2);
xlabel('x axis')
ylabel('y axis')
I want from user to enter 3 variable function and then plot it as single variable but it gives an error like that:
Error using ==> plot
Conversion to double from inline is not possible.
Error in ==> assignment3 at 11
plot(x,g,'r--','linewidth',2);
title('x versus '+ fcn);
what is the problem :s
Thank you very much for your kind answers.
Yahya Aktaş
[SL: formatted question as text not code]
0 件のコメント
回答 (1 件)
Rupesh
2024 年 3 月 28 日
Hi Yahya,
I understand that you're looking to input a function involving three variables (x, y, z), convert it into a single-variable function by substituting y and z with x, and then plot this function over a specified range of x values. You've encountered an error due to directly attempting to plot an inline function object, which MATLAB cannot process as expected. To address your issue, I have made some small modifications to the code to get the desired results.
function plotUserFunction
% step 1Prompt the user for a function involving x, y, z
userFunc = input('Enter any function with variables x, y, and z: ', 's');
% step 2 Convert the User-inputted Function: Use MATLAB's ability to handle strings and convert them into anonymous functions.
% Substitute Variables: Directly replace y and z with x in the user's input.
modifiedFunc = strrep(strrep(userFunc, 'y', 'x'), 'z', 'x');
%step 3 Convert the modified function string into an anonymous function
g = @(x) eval(vectorize(modifiedFunc));
% Define the range of x
x = 0:0.1:20;
% step 4Evaluate the Function: Use MATLAB's vectorized operations to evaluate the newly created single-variable function over the specified range of x values.
y = arrayfun(g, x);
% Step 5 Plot the Fun: Plot the evaluated function values against x
figure; plot(x, y, 'r--', 'LineWidth', 2);
xlabel('x axis'); ylabel('y axis');
title('Plot of the modified function');
end
As you can observe in the above code, a key aspect of achieving the solution involves the use of MATLAB's vectorized operations to evaluate the newly created single-variable function across the specified range of x values. This is done through the line fourth step, which applies the function `g`—our single-variable function—to each element within the array of x values.
You can refer to the below doc for a better understanding of the arrayfun function used in the above script.
Hope it helps!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Function Creation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!