4

How can I create vector function handle (anonymous function) with product a scalar value, then plot result. Clearly, I want to plot this formula

Let f(x,ym)= (1-exp(x-ym)/h); and 
iX(x)=1/6.f(x,ym(1))+
      7/6.f(x,ym(2))+
     25/6.f(x,ym(3))+
     43/6.f(x,ym(4));

iY(x)=1/5.f(x,ym(1))+
      2/5.f(x,ym(2))+
     14/5.f(x,ym(3))+
      4/5.f(x,ym(4));

This code without loop but give me :'' Error using plot Conversion to double from sym is not possible''.

clear all;clc;a=0;b=2;n=3;h=(b-a)/n;ym=a:h:b;                                      
X=[ 1/6, 7/6, 25/6,43/6];  % Data 
Y=[ 1/5, 2/5, 14/5,14/5];                                                   
f=@(x,ym) (1-exp(x-ym)/h);                                                              
% with syms 
syms x; 
iX=f(x,ym(1:length(ym))).*X; % result iX= [f.1/6 , f.7/6 , f.25/6 , f.43/6]
iY=f(x,ym(1:length(ym))).*Y;
iFx= sum(iX);iFy=sum(iY);   % result  iFx= f.1/6 + f.7/6 + f.25/6 + f.43/6
x=ym;
plot(x,iFx,'r+');hold on;plot(x,iFy,'og');

Moreover, I tired to use a alternate codes with loop condition but the error persists as conversion to logical from sym is not possible.

% clear all;clc
% a=0;b=2;n=3;h=(b-a)/n;ym=a:h:b; 
% %
% X=[ 1/6, 7/6, 25/6,43/6]; % Data
% Y=[ 1/5, 2/5, 14/5,4/5];
% %   
% f=@(x,ym) (1-exp(x-ym)/h);                
% %
% syms x  %double(subs(x))
% iX=cell(1,length(ym));iY=cell(1,length(ym)); %  cell, iX{k} and iY{k} 
% % iX=zeros(1,length(ym));iY=zeros(1,length(ym));
% for k=1:length(ym)           
% %          iX(k) =   f(x,ym(k)).*X(k);
% %          iY(k) =   f(x,ym(k)).*Y(k);  
%            iX{k} = @(x,k)  f(x,ym(k)).*X(k);
%            iY{k} = @(x,k)  f(x,ym(k)).*Y(k);
%            iX(x,k);
% end       
% whos 
% x=ym; 
% plot(x,iX{:},'r:');hold on;plot(x,iY{:},'b--'); 

please, how can I fix it?

workwolf
  • 53
  • 6

1 Answers1

1

You can't directly call plot(x,iFx) because iFx (and iFy) are symbolic variables depending on x and not the output from actually puting some real values into them. The easiest way is to fix this is to a) convert the symbolic variables to symbolic functions using symfun(formula,variables) and b) to then call said function with the values you want to plot them against:

clear all;clc;a=0;b=2;n=3;h=(b-a)/n;ym=a:h:b;                                      
X=[ 1/6, 7/6, 25/6,43/6];  % Data 
Y=[ 1/5, 2/5, 14/5,14/5];                                                   
f=@(x,ym) (1-exp(x-ym)/h);                                                              
% with syms 
syms x; 
iX=f(x,ym(1:length(ym))).*X; % result iX= [f.1/6 , f.7/6 , f.25/6 , f.43/6]
iY=f(x,ym(1:length(ym))).*Y;
iFx= sum(iX);iFy=sum(iY);   % result  iFx= f.1/6 + f.7/6 + f.25/6 + f.43/6


% same code until here

% step a) turn to symbolic function
iFx = symfun(iFx,x); iFy = symfun(iFy,x);

% step b) call the function with the "x" value of the plot.
plot(ym,iFx(ym),'r+');
hold on;
plot(ym,iFy(ym),'og');

Edit:

Sure, you can do the same thing with matlabFunction:

iFx = matlabFunction(iFx); iFy = matlabFunction(iFy);

For a discussion about symfun vs matlabFunction I refer to: matlab symfun vs anonymous function

TL;DR : matlabFunction returns an anonymous function which is usually faster, but symbolic functions are more flexible. But in this case it won't matter either way because the function is quite simple and the number of evaluated data points very little; therefore it will compute almost instantly anyways. But it's probably good practice to use matlabFunction.

Leander Moesinger
  • 2,449
  • 15
  • 28
  • +1, Many thanks. Can we use `matlabFunction(f)` instead of `symfun(formula,variables)`? please, any ideas why I should use `symfun(formula,variables)` or where it is more practical than `matlabFunction(f)` or other ? Again, thank you :) – workwolf Dec 09 '17 at 17:02