Hi to everyone:
i have four equations and four unknowns which is given below:
s*x+4y-3z+12m=100
2x+3*s*y+4z+200m=200
3x+y+z+4*s*m=150
25x+20y+10*s*m=250
where x,y,m znd z is unknown and s is given in excel sheet .
i want to find above unknowns by using s data which is given in excel sheet and than plot(x,s):
i have written code in matlab but it gave me error:
clc;
close all;
clear all;
syms x y m z
myData = readtable('batorrrkhaan.xlsx','Sheet','Taxila-hour')
s = myData.solar_radiation
iThreshold = 200
iKeep = s >= iThreshold
s = s(iKeep)
eqn1=s.*x+4*y-3*z+12*m==100;
eqn2=2*x+3*s.*y+4*z+200*m==200;
eqn3=3*x+y+z+4*s.*m==150;
eqn4=25*x+20*y+10*s.*m==250;
[x,y,t,z]=vpasolve([eqn1, eqn2, eqn3,eqn4],[x,y,t,z]);
x
y
t
z
error is :
z =
Empty sym: 0-by-1
x=
Empty sym: 0-by-1
m=
Empty sym: 0-by-1
y=
Empty sym: 0-by-1

 Réponse acceptée

eqn1=s.*x+4*y-3*z+12*m==100;
That does not define one equation: it defines one equation for each value of s.
[x,y,t,z]=vpasolve([eqn1, eqn2, eqn3,eqn4],[x,y,t,z]);
That is length(s) times 4 equations being solved for 4 variables. There are no scalar values of x, y, t, z that are able to satisfy all of the equations simultaneously.
solve() does not know that you mean that you want to solve for x, y, z, t per s value. Using one row of equations per s value will not help: solve() treats the set of equations as if you had done reshape(equations, [], 1)
You need to separate the equations yourself.
[x, y, t, z] = arrayfun(@(E1, E2, E3, E4) vpasolve([E1, E2, E3, E4], [x, y, t, z]), eqn1, eqn2, eqn3, eqn4)

13 commentaires

let suppose s has values in range of (J2:J100).
i want to find values of x,y,m and z by using data of s which is import from excel.
Dear Walter Reberson:
s values we will import from below excel file and other four unknowns we will find from equations than we plot(x,s).
i need solution of x,y,m and z for each value of s.
than i want to plot(x,s).
Okay, so read the s data in using readtable or xlsread() . And then proceed from just after your code line
s = myData.solar_radiation
but with the change I show here to use arrayfun.
thanks from your reply W.R.
if you give some information about E1,E2,E3 and E4 which yu have written in last comment.
this is my code what should i include ?
clc;
close all;
clear all;
syms x y m z
myData = readtable('batorrrkhaan.xlsx','Sheet','Taxila-hour')
s = myData.solar_radiation
iThreshold = 200
iKeep = s >= iThreshold
s = s(iKeep)
eqn1=s.*x+4*y-3*z+12*m==100;
eqn2=2*x+3*s.*y+4*z+200*m==200;
eqn3=3*x+y+z+4*s.*m==150;
eqn4=25*x+20*y+10*s.*m==250;
[x,y,t,z]=vpasolve([eqn1, eqn2, eqn3,eqn4],[x,y,t,z]);
x
y
t
z
plot(x,s)
hold on
plot(y,s)
hold on
plot(z,s)
Hi Walter:
the only confusion i have is E1,E2,E3,and E4 in below equation:
[x, y, t, z] = arrayfun(@(E1, E2, E3, E4) vpasolve([E1, E2, E3, E4], [x, y, t, z]), eqn1, eqn2, eqn3, eqn4).
should i put equations instead of E1,E2,E3 and E4 or i should put variables .
thank you so much .
dear Walter:
this is error :
Error using arrayfun
All of the input arguments must be of the same size and shape.
Previous inputs had size 1 in dimension 1. Input #3 has size 2891
Error in neww (line 15)
[x,y,t,z] = arrayfun(@(x,s)vpasolve([s*x+4*y-3*z+12*t-100, 2*x+3*s.*y+4*z+200*t-200, 3*x+y+z+4*s.*t-150,25*x+20*y+10*s.*t-250]), [x, y, t, z],
eqn1, eqn2, eqn3, eqn4)
thanks
E1, E2, E3, E4 are argument names for the anonymous function.
[x, y, z, m] = arrayfun(@(E1, E2, E3, E4) vpasolve([E1, E2, E3, E4], [x, y, z, m]), eqn1, eqn2, eqn3, eqn4);
is nearly the same as:
xout = zeros(size(eqn1), 'sym');
yout = zeros(size(eqn1), 'sym');
zout = zeros(size(eqn1), 'sym');
mout = zeros(size(eqn1), 'sym');
for K = 1 : numel(eqn1)
E1 = eqn1(K);
E2 = eqn2(K);
E3 = eqn3(K);
E4 = eqn4(K);
[xout(K), yout(K), zout(K), mout(K)] = vpasolve([E1, E2, E3, E4], [x, y, z, m]);
end
x = xout; y = yout; z = zout; m = mout;
clear E1 E2 E3 E4 K xout yout zout mout
During execution of the anonymous function, the first parameter passed to the anonymous function is known by the name E1, the second parameter is known by E2, the third parameter is known by E3, the fourth parameter is known as E4. The body of the anonymous function is executed with those values.
E1, E2, E3, E4 have no independent existence: they stand in for "what was passed as the first parameter", "what was passed as the second parameter" and so on.
That approach with vpasolve is just too slow. Here is a much faster version:
if ispc()
filename = 'batorrrkhaan.xlsx';
varname = 'solar_radiation';
else
filename = 'Taxila_TMY1.xlsx';
varname = 'Direct normal Radiation [Wh/m^2]';
end
syms x y m z
myData = readtable(filename, 'Sheet', 'Taxila-hour', 'PreserveVariableNames', true);
s = myData.(varname);
iThreshold = 200;
iKeep = s >= iThreshold;
s = s(iKeep);
syms S
eqn1 = S*x + 4*y - 3*z + 12*m == 100;
eqn2 = 2*x + 3*S*y + 4*z + 200*m == 200;
eqn3 = 3*x + y + z + 4*S*m == 150;
eqn4 = 25*x + 20*y + 10*S*m == 250;
sol = solve([eqn1, eqn2, eqn3, eqn4], [x, y, z, m]);
x = double(subs(sol.x, S, s));
y = double(subs(sol.y, S, s));
z = double(subs(sol.z, S, s));
m = double(subs(sol.m, S, s));
I had to guess about which variable name in the file you posted corresponded to solar radiation.
You will need to adjust the filename and varname for your actual situation.
thank you so much for your concentration :
i will try above code .
Error using readtable (line 198)
Invalid parameter name: PreserveVariableNames.
Error in teching (line 10)
myData = readtable(filename, 'Sheet', 'Taxila-hour', 'PreserveVariableNames', true);
PreserveVariableNames ?
Either you have a broken MATLAB installation or else you forgot to tell us that you using an old release of MATLAB.
Remove the 'PreserveVariableNames', true option from the call. You may need to adjust the name in the 'varname' assignment, such as to 'DiffuseHorizontalRadiation_Wh_m_2_'
thanks you so much Dear Walter reberson:
i have no words about you , you have solved my problem.
once again thank you so much .

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by